struts2学习笔记7-其它

1.struts2模块化的配置方式


<include file="struts_1.xml"></include>
<include file="struts_2.xml"></include>


2.模型驱动
如下

public class StrutsAction extends ActionSupport implements ModelDriven<UserView>{
private UserView userView = new UserView();
//提供GET方法
public UserView getModel() {
return userView;
}

public String userModelDrivenMethod(){
return Action.SUCCESS;
}
@Override
public void validate() {

}
}
public class UserView {
private String username;
private String password;
private int age;
//set and get method
}
//struts2驱型驱动要求modelDriven这个拦截器必须在参数拦截器之前,但是默认就是在之前,所以可以不要下面这样的配置


<action name="model" class="com.langhua.action.StrutsAction" method="userModelDrivenMethod">
<result name="success">/modelsuccess.jsp</result>
<result name="input">/modelsuccess.jsp</result>
<interceptor-ref name="modelDriven"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>


3.fieldError只显示一个字段的错误

<s:fielderror>
<s:param>username</s:param>
</s:fielderror>


4.Struts2的防止表单重复提交
首先在JSP页面中的表单中,要用到Struts2的标签

<form action="/struts2/langhua/model.action" method="post">
<s:token name="tokenValueName"></s:token>
<input type="text" name="username"/><br/>
<input type="password" name="password"/><br/>
<input type="text" name="age"/><br/>
<input type="submit" value="submit"/>
</form>

这样会生成一个隐藏域,保存着和服务器上同步的值,就像验证码一样
然后在这个Action中配置如下

<action name="model" class="com.langhua.action.StrutsAction" method="userModelDrivenMethod">
<result name="success">/modelsuccess.jsp</result>
<result name="input">/modelsuccess.jsp</result>
<result name="invalid.token">/modelsuccess.jsp</result>
<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="modelDriven"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>


invalid.token表示重复提交之后要到的页面
token是Struts2提供的拦截器

5.异常处理
局部异常

<exception-mapping result="usernameException" exception="com.langhua.exception.UsernameException"></exception-mapping>
<result name="usernameException">/usernameException.jsp</result>


全局异常

<global-exception-mappings>
<exception-mapping result="xxx" exception="xxxx.xxxx"></exception-mapping>
</global-exception-mappings>


6.常用的返回结果result的type
dispatcher转发
redirect重定向
redirectAction重定向到一个Action
stream到一个流,如文件下载
chart返回jfreechart格式
plainText返回文本格式,如XML,TXT
json返回Json格式
有的要参数可以在相应的类文件下面找
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值