一.struts2对异常的处理
1.自定义局部异常:
<action> <exception-mapping result="sonException" exception="java.lang.ArithmeticException"></exception-mapping> </action>
2.自定义全局异常:
<!-- 配置全局异常处理 --> <global-exception-mappings> <exception-mapping result="sonException" exception="java.lang.ArithmeticException"></exception-mapping> </global-exception-mappings>
3.异常执行的优先级
>>当同时出现全局和局部异常时,局部异常优先,即局部异常优先于全局异常执行,如果全局和局部异常相同,那么将以局部异常为准.
>>当异常出现父子关系时,子异常优先.如java.lang.ArithmeticException是java.lang.Exception的子类.如果同时进行配置的话,那么以java.lang.ArithmeticException为准.
如:
<!-- 配置全局异常处理 --> <global-exception-mappings> <exception-mapping result="sonException" exception="java.lang.ArithmeticException"></exception-mapping> <exception-mapping result="fatherException" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings>
<!-- 配置全局结果处理 --> <global-results> <result name="success" type="dispatcher"> /WEB-INF/success.jsp </result> <result name="sonException" type="dispatcher"> /WEB-INF/sonException.jsp </result> <result name="fatherException" type="dispatcher"> /WEB-INF/fatherException.jsp </result> </global-results>
二.struts2中的类型转换
1、内置类型转换器
HTTP没有类型的概念,只能将表单的参数,以String或String[]的方式接收.
>>1).struts2提供了内置的类型转换器,它能将string类型和8种基本类型自动转换.
>>2).struts2的内置类型转换器,能将java.util.Date与String类型自动转换,但Date类型的字符串格式必须为yyyy-MM-dd的格式;同时struts2会自动检测日期的合法性,如01-33这种就不能通过.
>>3).内置转换器,不能将String类型转换成任意javaBean类型,这里需要使用自定义转换器.
下面以实例来演示:
Address.java
package type;
/**
* @ClassName: Address
* @Description: 定义一个address model
* @author: amosli
* @email:amosli@infomorrow.com
* @date Jan 26, 2014 1:53:33 AM
*/
public class Address {
private String province;// 省份
private String city;// 城市
private String area;// 区域
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
TypeAction.java
package type; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; /** * @ClassName: TypeAction * @Description: 本类主要是讲解类型转换 * @author: amosli * @email:amosli@infomorrow.com * @date Jan 26, 2014 1:00:35 AM */ public class TypeAction extends ActionSupport{ private static final long serialVersionUID = -2558635842755603468L; private String username;//姓名 private Integer age;//年龄 private Float salary;//薪水 private Date birthday;//生日 private Address address;//地址 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public void setAge(Integer age) { this.age = age; } public Float getSalary() { return salary; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public void setSalary(Float salary) { this.salary = salary; } public String execute() throws Exception { return SUCCESS; } }
type_struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="type" extends="struts-default"> <action name="TypeAction" class="type.TypeAction" method="execute"> <result name="success" type="dispatcher"> /WEB-INF/type_success.jsp </result> <result name="input" type="dispatcher"> type.jsp </result> </action> </package> </struts>
sturts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!--<include file="config/upload.xml"></include> --> <!-- 加载其他配置文件 --> <!-- <include file="config/upload-interceptor.xml"></include> --> <!-- 加载属性文件-国际化 --> <!-- <constant name="struts.custom.i18n.resources" value="message"></constant> --> <!-- 结果集 --> <!-- <include file="config/result_struts.xml"></include> --> <!-- 类型转换 --> <include file="config/type_struts.xml"></include> </struts>
type.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:form action="TypeAction" type="POST"> <s:textfield label="用户名" name="username" /> <s:textfield label="年龄" name="age" /> <s:textfield label="薪水" name="salary" /> <s:textfield label="生日" name="birthday"/> <s:textfield label="地址" name="address"/> <s:submit name="submit"/> </s:form> </body> </html>
type_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 恭喜,类型转换成功! <hr> 用户名:<s:property value="username"/><br> 年龄:<s:property value="age"/><br> 薪水:<s:property value="salary"/><br> 生日:<s:property value="birthday"/><br> 地址:<s:property value="address"/><br> </body> </html>
效果截图:
1),不填写地址
2),查看结果,基本类型的数据自动转换成功
3),填写地址
4),这里配置了result中的input,一时出错页面将转发到type.jsp,并使用标签自带的错误消息回显.