24H免费课程咨询  TEL:13401595960   QQ:1870218756  微信:13401595960(李老师)

东方博宜

网站首页 > 软件开发资讯 > Java开发

struts框架基础2:模糊匹配和通过实现接口获取作用域

2017-07-11 20:38:11 东方博宜 阅读

1、模糊匹配

例子:一个参数的模糊匹配

index.jsp

    <a href="getValue.action">获取参数信息</a>

 

UserAction

public class UserAction {

 

    public String getValue(){

      

       return "success";

    }

}

 

struts.xml

   <package name="mystruts2" extends="struts-default" namespace="/">

     

      <!-- 获取参数的请求,采用模糊匹配,*代表任意的请求,{1}代表*的值 -->

      <action name="*" class="com.user.action.UserAction" method="{1}">

         <result name="success" type="dispatcher">/main.jsp</result>

      </action>

   </package>

 

例子:多个参数的模糊匹配

Index.jsp

    <a href="getValue_main.action">获取参数信息</a>

    <br />

    <a href="getList_userlist.action">获取列表数据</a>

 

UserAction:

public class UserAction {

 

    public String getValue(){

      

       return "success";

    }

   

    public String getList(){

      

       return "success";

    }

}

 

struts.xml

   <package name="mystruts2" extends="struts-default" namespace="/">

     

      <!-- 获取参数的请求,采用模糊匹配,*代表任意的请求,{1}代表*的值 -->

      <action name="*_*" class="com.user.action.UserAction" method="{1}">

         <result name="success" type="dispatcher">/{2}.jsp</result>

      </action>

   </package>

 

 

2、通过实现ServletRequestAwareServletResponseAwareServletContextAware来注入requestresponseapplication的值!

实现思路:定义CommonAction专门用来获取作用域的值(获取后将作用域作为属性),需要的Action直接继承CommonAction相当于都将作用域作为了自己的属性!

 

CommonAction

/**

 * 获取作用域的值,并将作用域作为属性

 */

public class CommonAction  implements ServletRequestAware,ServletResponseAware,ServletContextAware{

 

    //将作用域定义为属性

    public HttpServletRequest request;

    public HttpSession session;

    public ServletContext application;

    public HttpServletResponse response;

   

    public void setServletRequest(HttpServletRequest request) {

       this.request = request;

       this.session  = request.getSession();

    }

 

    public void setServletResponse(HttpServletResponse response) {

       this.response = response;

    }

 

    public void setServletContext(ServletContext application) {

       this.application = application; 

    }

}

 

index.jsp

    <a href="getValue.action">获取参数信息</a>

 

UserAction

public class UserAction extends CommonAction{

 

    //获取参数

    public String getValue(){

       request.setAttribute("username", "张晓峰");

       session.setAttribute("password", "123456");

       application.setAttribute("bignews", "今晚有雨!");

      

       return "success";

    }

}

 

main.jsp

    request${requestScope.username}

    <br />

    session${sessionScope.password}

    <br />

    application${bignews}

<br />

 

struts.xml

   <package name="mystruts2" extends="struts-default" namespace="/">

     

      <!-- 获取参数的请求,采用模糊匹配,*代表任意的请求,{1}代表*的值 -->

      <action name="getValue" class="com.user.action.UserAction" method="getValue">

         <result name="success" type="dispatcher">/main.jsp</result>

      </action>

   </package>

图片.png

Powered by 东方博宜教育咨询江苏有限公司  ©2008-2017 www.czos.cn