Struts2学习笔记三(异常处理、模型驱动和属性驱动、servlet接口、Preparable接口)

1、自定义异常:

       public class UsernameException extends Exception

	public String execute() throws Exception {
		
		if(!"123".equals(username))
		{
			throw new UsernameException("username invalid");
		}
		if(!"123".equals(password))
		{
			throw new PasswordException("password invalid");
		}
		
		return SUCCESS;
	}

2、接受异常,然后跳转。

		<action name="login" class="com.yanlei.struts2.LoginAction">
			<result name="success">/result.jsp</result>
<span style="color:#ff0000;">			<exception-mapping result="</span><span style="color:#00cccc;">usernameInvild</span><span style="color:#ff0000;">" exception="com.yanlei.exception.UsernameException"></exception-mapping>
			<exception-mapping result="passwordInvild" exception="com.yanlei.exception.PasswordException"></exception-mapping>
			<result name="</span><span style="color:#00cccc;">usernameInvild</span><span style="color:#ff0000;">">/usernameInvild.jsp</result>
			<result name="passwordInvild">/passwordInvild.jsp</result></span>
		</action>
3、也可定义在全局内,可以被所有action共用。

	<global-results>
			<result name="usernameInvalid">/usernameInvalid.jsp</result>
			<result name="passwordInvalid">/passwordInvalid.jsp</result>
	</global-results>
		
	<global-exception-mappings>
		<exception-mapping result="usernameInvalid" exception="com.shengsiyuan.exception.UsernameException"></exception-mapping>
		<exception-mapping result="passwordInvalid" exception="com.shengsiyuan.exception.PasswordException"></exception-mapping>
	</global-exception-mappings>
  但局部总是优先于全局的。


4、模型驱动如写个User类,实现modelDriven<Uset>,重写public User getMedel(){return user;}

      但一般都使用属性驱动的模式。直接写属性,不用写类。。

5、Servlet两种方式

HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();
session.setAttribute("hello", "struts");


ActionContext actionContext=ActionContext.getContext();
Map<String, Object> map=actionContext.getSession();
Object o=map.get("hello");

6、在传值之前做准备,action实现Preparable接口。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值