一、新建项目
1,新建web项目~
2,修改web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>仔细观察可发现此过滤器使用的是StrutsPrepareAndExecuteFilter,之前项目使用的是FilterDispathcer
,他们有什么不同呢?org.apache.struts2.dispatcher.FilterDispatcher) 在早期的Struts2开发中使用,从Struts
2.1.3开始,它已不推荐使用。package com.etoak.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionSupport;
public class AllAction extends ActionSupport {
/*
* 在struts1和struts2中都存在一个功能就是
* 表单项封装成什么数据类型就可以向那个数据类型
* 转换,但是struts1中如果出现了转换失败则
* 导致框架崩溃500,而struts2中如果出现转化失败
* 则自动添加一个字段级错误提示信息告诫用户填写不符合
* 要求,不会导致框架崩溃
* */
private Integer tid;
private String tname;
private String tpass;
private Integer tage;
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getTpass() {
return tpass;
}
public void setTpass(String tpass) {
this.tpass = tpass;
}
public Integer getTage() {
return tage;
}
public void setTage(Integer tage) {
this.tage = tage;
}
public String addTea() throws Exception {
System.out.println("thisisaddTea~~~~");
return this.SUCCESS;
}
public String delTea() throws Exception {
System.out.println("thisisdelTea~~~~");
return this.SUCCESS;
}
public String checkTea() throws Exception {
System.out.println("thisischeckTea~~~~");
return this.SUCCESS;
}
/*
* 使用一个总的validat方法验证
* 所有请求
* */
@Override
public void validate() {
ActionContext ac = ActionContext.getContext();
//拿到ActionInvocation
ActionInvocation ai = ac.getActionInvocation();
ActionProxy ap = ai.getProxy();
//拿到struts.xml中的method属性值
String method = ap.getMethod();
if(method.equals("addTea")){
if(this.getTage()==null){
this.addFieldError("tage","年龄输入不合法");
}else if(this.getTage()<25){
this.addFieldError("tage", "年龄不得小于25岁");
}
if(this.getTname().trim().length()<4||
this.getTname().trim().length()>8){
this.addFieldError("tname","用户名不能小于4位大于8位");
}
if(this.getTpass().trim().length()<4||
this.getTpass().trim().length()>8){
this.addFieldError("tpass","用户密码不能小于4位大于8位");
}
}else if(method.equals("delTea")){
if(this.getTid()==null){
this.addFieldError("tid","删除id输入不合法");
}
}
}
/*
* 验证方法命名方式
* validate+method属性值 method属性值
* 首字母大写
*
* */
// public void validateAddTea() {
// if(this.getTage()==null){
// this.addFieldError("tage","年龄输入不合法");
// }else if(this.getTage()<25){
// this.addFieldError("tage", "年龄不得小于25岁");
// }
//
// if(this.getTname().trim().length()<4||
// this.getTname().trim().length()>8){
// this.addFieldError("tname","用户名不能小于4位大于8位");
// }
//
// if(this.getTpass().trim().length()<4||
// this.getTpass().trim().length()>8){
// this.addFieldError("tpass","用户密码不能小于4位大于8位");
//
// }
// }
// public void validateDelTea() {
// if(this.getTid()==null){
// this.addFieldError("tid","删除id输入不合法");
// }
// }
}
4,新建struts.xml,介绍跳转指定方法的三种方式
<package name="method" extends="struts-default">
<!-- 跳转到指定方法的第一种方式,添加属性method,并指定具体属性值。
路径:/addTea/add
缺点:不够灵活,多个方法需要写多个action -->
<action name="addTea" class="com.etoak.action.AllAction"
method="add">
<result name="input">/addTea.jsp</result>
<result>/addsuc.jsp</result>
</action>
<!-- 第二种方式:动态方法调用DMI(Dynamic Method Invocation)
这种方式就是在调用的路径中用“!方法名”的方式调用指定方法
路径:/addTea!add
特点:灵活。只要指定不同的方法就可以做不同的操作。 -->
<action name="addTea" class="com.etoak.action.AllAction">
<result name="input">/addTea.jsp</result>
<result>/addsuc.jsp</result>
</action>
<!--
第三种方式:通配符,通过传过来的路径匹配name属性的*号匹配后面的{1},如果name="*Tea*",那后面就会有{1},{2}对应的匹配。
路径:/addTea
特点:更加灵活。整个项目甚至只要配一个总的action。是指定方法的一个特殊的用法。不过我觉得用DMI可以将每个模块分的清楚。
-->
<action name="*Tea" class="com.etoak.action.AllAction"
method="{1}Tea">
<result name="input">/{1}Tea.jsp</result>
<result>/{1}suc.jsp</result>
</action>
</package>
7446



被折叠的 条评论
为什么被折叠?



