1.
@ExceptionHandler(ArithmeticException.class)
public ModelAndView getArithmeticException(Exception ex){
ModelAndView mv = new ModelAndView("error");
mv.addObject("ex", ex);
return mv;
}
@RequestMapping("/zero")
public void ac(@RequestParam("i") int i){
System.out.println(10/i);
}
当发生ArithmeticException异常的时候,在error.jsp页面输出异常 。
2.
@ControllerAdvice
public class Exceptions {
@ExceptionHandler(ArithmeticException.class)
public ModelAndView getArithmeticException(Exception ex){
ModelAndView mv = new ModelAndView("error");
mv.addObject("ex", ex);
return mv;
}
}
如果在本类中找不到异常处理的方法,就去@ControllerAdvice注解的类中查找异常处理的类的方法。
3.
@ResponseStatus(value=HttpStatus.BAD_REQUEST,reason="请求不对")
public class UserExceptions extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = 1L;
}
在controller的方法里抛出UserExceptions 异常,在页面上显示
HTTP Status 400 - 请求不对
(function (){
function Person(){
this.getAge=function (age){
alert(age);
}
}
//让此类的所有对象有name属性,showName方法
Person.prototype.name="yjw";
Person.prototype.showName=function(){
alert(this.name);
}
// new Person().showName();
var v={};
//让空对象v继承Person,含有Person的所有属性和方法
v.__proto__=new Person();
v.__proto__.constructor=v;
v.getAge(1);
v.showName();
})()
(function (){
//创建一个人员类
function Person(n){
this.name=n;
this.getAge=function (age){
alert(age);
}
}
//创建教师类
function Teacher(name,books){
//call方法可以将一个函数的对象上下文,从初始化变成由this来决定
//调用Person的构造函数,
Person.call(this, name);//this就是person的this,name属性就是person的属性
this.books=books;
}
//让教师类继承人员类
Teacher.prototype=new Person();
Teacher.prototype.constructor=Teacher;//教师的构造方法还是使用教师自己的构造方法
Teacher.prototype.getBook = function(){//给教师类的原型添加方法
return this.name+" "+this.books;
}
var jim = new Teacher("jim","extjs");
// alert(jim.getBook());
//jim.getAge(2);
function extend(subClass,superClass){
//1.让子类原型类属性等于父类的原型属性,初始化一个中间空对象,为了转换主父类关系
var f = function(){};
f.prototype=superClass.prototype;
//2.让子类继承f
subClass.prototype=new f();
subClass.prototype.constructor=subClass;
//3.为子类增加属性,继承父类的原型对象
subClass.fuLei=superClass.prototype;
//4.增加一个保险,就算父类的原型类是超类object,也要把父类的构造函数的级别降下来
if(superClass.prototype.constructor==Object.prototype.constructor){
superClass.prototype.constructor=superClass;
}
}
function Student(name,books){
Student.fuLei.constructor.call(this,name);
this.books=books;
this.getBooks=function(){
return this.name+" "+this.books;
}
}
extend(Student, Person);
var s = new Student("s","a");
alert(s.getBooks());
s.getAge(2);
})()
1.如果两个网站域名的一级域名相同,可以使用cookie和filter实现单点登录,因为网站有可能(具体看cookie的设置)可以共享cookie。例如:www.bbs.aa.cn www.news.aa.cn。
第一个网站在登录后,把用户信息写到cookie中,当访问第二个网站时,第二个网站先经过自己的filter,检查session,如果没有,查询cookie,取出用户信息,放在session中登录。
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
if(request.getSession().getAttribute("user")== null){
Cookie[] cs = request.getCookies();
if (cs != null && cs.length > 0) {
for (Cookie c : cs) {
String cName = c.getName();
if (cName.equals("sso")) {
String userName = c.getValue();
request.getSession().setAttribute("user", userName);
}
}
}
}
chain.doFilter(request, resp);
}
2.如果两个网站域名的一级域名不同,不可以使用cookie和filter实现单点登录,因为网站不可以共享cookie。例如:www.bbs.cn www.news.cn。
使用cas框架服务实现单点登录。1.部署cas服务端。2.在服务器端的ticketGrantingTicketCookieGenerator.xml中修改文件。<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"//使用http协议
p:cookieMaxAge="-1"//cookie有效时间
p:cookieName="yjwname"//cookie名称
p:cookiePath="/" />//项目名称
3.部署www.bbs.cn www.news.cn服务,在每个客户端项目中加入casjar包,在web.xml中配置<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2008, Martin W. Kirst
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Martin W. Kirst nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<web-app id="mywebapp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>mywebapp</display-name>
<description>
Simple sample, how to use CAS Java Client 3.x.
In this sample exists a public area (/)
and a private area (/protected/*).
</description>
<!-- Sign out not yet implemented -->
<!--
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
</filter>
-->
<filter>
<filter-name>CAS Authentication Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<!--cas服务器地址-->
<param-name>casServerLoginUrl</param-name>
<param-value>http://www.service.com:8081/login</param-value>
</init-param>
<init-param>
<!--自己的地址-->
<param-name>serverName</param-name>
<param-value>http://www.bbs.com:8081</param-value>
</init-param>
<init-param>
<param-name>renew</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>gateway</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>http://www.service.com:8081</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://www.bbs.com:8081</param-value>
</init-param>
</filter>
<filter>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>
<filter>
<filter-name>CAS Assertion Thread Local Filter</filter-name>
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>
<!-- ************************* -->
<!-- Sign out not yet implemented -->
<!--
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<filter-mapping>
<filter-name>CAS Authentication Filter</filter-name>
<!--此URL下的资源都需要验证登录-->
<url-pattern>/protected/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Assertion Thread Local Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/proxyCallback</url-pattern>
</filter-mapping>
<!-- *********************** -->
<!-- Sign out not yet implemented -->
<!--
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>
-->
</web-app>
4.启动服务,这样就可以实现单点登录