struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"></include>
//cjhstruts.xml //省略
<include file="cjhstruts.xml"></include> <!-- 引用加一个xml文件(这样在一个大的项目中如果action太多,可以分模块放) -->
<!-- package中的namespace表示命名空间如果设置了,刚在页面用搞用??.action要加上 命名空间/??.action 比如 mm/test1.action-->
<package name="test" extends="struts-default"> <!-- extends:表示继承父类struts-default -->
<interceptors>
<!-- 定义拦截器 所定义的类要继承AbstractInterceptor类 -->
<interceptor name="interceptor1" class="com.yysoft.interceptors.Interceptors1"></interceptor>
<interceptor name="methodFilterInterceptor1" class="com.yysoft.interceptors.MethodFilterInterceptor1">
<!-- exculdeMethods:是不被拦截的方法,如果有多个以逗号分隔。
includeMethods:需要被拦截的方法,如果有多个以逗号分隔。-->
<!--<param name="exculdeMethods">execute</param>-->
<!--<param name="includeMethods">execute</param>-->
</interceptor>
<interceptor-stack name="mydefalut"> <!-- 定义默认拦截器 mydefalut为固定的 -->
<interceptor-ref name="defaultStack"></interceptor-ref> <!-- struts默认的 defaultStack为固定的-->
<interceptor-ref name="interceptor1" /> <!-- 需求添加的默认拦截器 -->
</interceptor-stack>
</interceptors>
<!-- 定义全局的反回页面 -->
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<!-- 说明用,没有作用 -->
<!--<global-results>
<result name="mainMenu" type="redirect">mainMenu.html</result>
<result name="dataAccessFailure">pages/dataAccessFailure.jsp</result>
</global-results>-->
<!--定义异常处理 exception指定异常 result当发生该异常时进放到那个页面进行处理 -->
<!--<global-exception-mappings>
<exception-mapping exception="org.springframework.dao.DataAccessException" result="dataAccessFailure"/>
</global-exception-mappings>-->
<action name="test1" class="com.yysoft.action.Test1Action" method="execute">
<!--<interceptor-ref name="interceptor1"></interceptor-ref>-->
<!--<interceptor-ref name="methodFilterInterceptor1"></interceptor-ref>-->
<!--<interceptor-ref name="defaultStack"></interceptor-ref>-->
<result name="success">/success.jsp</result>
<result name="failure" type="redirect">/failure.jsp</result>
<result name="login" type="redirect">/failure.jsp</result> <!-- 当登录出错时返回到这 return Action.LOGIN; -->
</action>
<action name="test2" class="com.yysoft.action.Test2Action">
<result name="success">/success.jsp</result>
<result name="failure" type="redirect">/failure.jsp</result>
</action>
<action name="validate" class="com.yysoft.action.ValidateAction">
<result name="success">/validateOK.jsp</result>
<result name="failure">/validateNO.jsp</result>
<result name="input">/validateNO.jsp</result>
</action>
</package>
</struts>
以下是所配置的action
一:最简单的action
package com.yysoft.action;
public class Test1Action {
private int t1id;
private String t1name;
public int getT1id() {
return t1id;
}
public void setT1id(int t1id) {
this.t1id = t1id;
}
public String getT1name() {
return t1name;
}
public void setT1name(String t1name) {
this.t1name = t1name;
}
public String execute(){
System.out.println("成功进入..."+t1id+" "+t1name);
if(t1name.length()>5){
System.out.println("长度大于5");
return "success";
}else{
System.out.println("长度小于或等于5");
return "failure";
}
}
}
二:再bean属性的action
public class Test2Action {
Users us;
public Users getUs() {
return us;
}
public void setUs(Users us) {
this.us = us;
}
public String execute(){
if(us.getUname().length()>5){
System.out.println("长度大于5");
return "success";
}else{
System.out.println("长度小于或等于5");
return "failure";
}
}
}
三:取得请求对象的action
public class Test3Action {
private String cc;
public void setCc(String cc) {
this.cc = cc;
}
public String getreq(){
ActionContext ac=ServletActionContext.getContext();
Map mapapplication = ac.getApplication();
Map mapsession = ac.getSession();
Map mapparam=ac.getParameters();
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
String reqid=request.getParameter("reqid");
String reqname=request.getParameter("reqname");
Object objcjh=mapsession.get("cjh");
Object objchen=mapsession.get("chen");
Object objo=mapsession.get("ok");
System.out.println("reqid"+reqid);
System.out.println("reqname"+reqname);
System.out.println("objcjh"+objcjh);
System.out.println("objchen"+objchen);
System.out.println("objo"+objo);
System.out.println("cc:"+cc);
return "success";
}
}
四:另一种取得请求对象的action
public class Test4Action implements ServletRequestAware, ServletResponseAware,
ApplicationAware, SessionAware {
Map mapsession,mapapplication;
ServletRequest request;
ServletResponse response;
public void setServletRequest(HttpServletRequest req) {
this.request=req;
}
public void setServletResponse(HttpServletResponse res) {
this.response=res;
}
public void setApplication(Map app) {
this.mapapplication=app;
}
public void setSession(Map sess) {
this.mapsession=sess;
}
public String gettest4(){
System.out.println("");
String reqid=request.getParameter("reqid");
String reqname=request.getParameter("reqname");
Object objcjh=mapsession.get("cjh");
Object objchen=mapsession.get("chen");
Object objo=mapsession.get("ok");
System.out.println("reqid"+reqid);
System.out.println("reqname"+reqname);
System.out.println("objcjh"+objcjh);
System.out.println("objchen"+objchen);
System.out.println("objo"+objo);
return "success";
}
public String gettest4_1(){
System.out.println("进放到gettest4_1");
return "success";
}
}
五:拦截器
public class Interceptors1 extends AbstractInterceptor{
//定义一个拦截器
@Override
public void init() {
System.out.println("初始化拦截器");
super.init();
}
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("进入到拦截器Interceptors1.java");
Map session = actionInvocation.getInvocationContext().getSession();
ServletRequest request=ServletActionContext.getRequest();
System.out.println(request.getParameter("t1id"));
System.out.println(request.getParameter("t1name"));
Object c=session.get("c");
if(c!=null){
System.out.println(c.toString());
return actionInvocation.invoke(); //进行后续操作。
}else{
System.out.println(Action.LOGIN);
return Action.LOGIN;
}
}
@Override
public void destroy() {
System.out.println("销毁拦截器");
super.destroy();
}
}