1.struts2模块化的配置方式
如
2.模型驱动
如下
3.fieldError只显示一个字段的错误
4.Struts2的防止表单重复提交
首先在JSP页面中的表单中,要用到Struts2的标签
这样会生成一个隐藏域,保存着和服务器上同步的值,就像验证码一样
然后在这个Action中配置如下
invalid.token表示重复提交之后要到的页面
token是Struts2提供的拦截器
5.异常处理
局部异常
全局异常
6.常用的返回结果result的type
dispatcher转发
redirect重定向
redirectAction重定向到一个Action
stream到一个流,如文件下载
chart返回jfreechart格式
plainText返回文本格式,如XML,TXT
json返回Json格式
有的要参数可以在相应的类文件下面找
如
<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格式
有的要参数可以在相应的类文件下面找