Struts2

Posted 57容杰龙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2相关的知识,希望对你有一定的参考价值。

以下内容是基于导入struts2-2.3.32.jar包来讲的

1.全局视图配置

xml标签:
<global-results> <result name="error">/error.jsp</result> </global-results>
 1 package com.ronng.web.action;
 2 
 3 public class TwoAction {
 4     public String show(){
 5         return "error";
 6     }
 7     
 8     public String look(){
 9         return "error";
10     }
11 }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="com.rong.web.action" namespace="/" extends="struts-default">
        <!-- 全局视图。当action标签里面不包含result标签时,就会寻找全局的global-results结果视图 -->
        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>
        <action name="error1" class="com.ronng.web.action.TwoAction" method="show">
        </action>
        <action name="error2" class="com.ronng.web.action.TwoAction" method="look">
        </action>
    </package>
    
</struts>

访问路径:

http://localhost:8080/struts/error1

http://localhost:8080/struts/error2

返回的都是同一个error.jsp页面

2.全局的异常配置

 1 package com.ronng.web.action;
 2 
 3 public class TwoAction {
 4     public String show() throws Exception{
 5         //抛出异常,无法返回"error"字符串
 6         int number=1/0;
 7         return "error";
 8     }
 9     
10     public String look()throws Exception{
11         return "error";
12     }
13 }

struts.xml配置是有顺序的:全局结果视图需要放在全局异常的前面

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="com.rong.web.action" namespace="/" extends="struts-default">
        
        <!-- 全局视图。当action标签里面不包含result标签时,就会寻找全局的global-results结果视图 -->
        <global-results>
            <result name="error">/error.jsp</result>
            <result name="exception">/exception.jsp</result>
        </global-results>
        <!-- 全局异常配置 。result对应全局视图的result的name-->
        <global-exception-mappings>
            <exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
        </global-exception-mappings>
        <action name="error1" class="com.ronng.web.action.TwoAction" method="show">
        </action>
        <action name="error2" class="com.ronng.web.action.TwoAction" method="look">
        </action>
    </package>
    
</struts>

以上的方法不能处理类似404的错误,若要处理404错误,则只需要修改web.xml配置文件

<error-page>标签配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>struts</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <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>
  
  <error-page>
      <error-code>404</error-code>
      <location>/404.jsp</location>
  </error-page>
</web-app>

3.最简单的action配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="com.rong.web.action" namespace="/" extends="struts-default">
        <!-- 最简单的action
            class 默认是struts-default.xml的  
                <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
            默认执行的method 是 execute
            方法默认的返回值是什么 "success"
         -->
        <!-- 对于最简单的action也就是
        没有配置class的action要执行的哪个action,
        可以不使用默认的,重新配置 ,但是依然会使用默认的 execute方法-->
        <default-class-ref class="com.ronng.web.action.OneAction"></default-class-ref>
        <action name="simple">
            <result>/index.jsp</result>
        </action>
    </package>
    
</struts>

4.路径访问搜索顺序

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- 
        资源路径
        http://localhost:8080/struts/search
        http://localhost:8080/struts/aa/bb/cc/search
        以上两种方式均可访问到同一页面
        其中http://localhost:8080/struts是服务器以及项目信息
            主要看这段:/aa/bb/cc/search
            搜索顺序:没找到时继续往前找,由于search是action里面的name路径,所以最后才判断
            先判断package,最后才判断action
                namespace是/aa/bb/cc
                namespace是/aa/bb
                namespace是/aa
                namespace是/
                有这个/的命名空间了
                    那就在这个命名空间的action里找search
     -->
    <!-- namespace命名空间不写时,默认是斜杠"/" -->
    <package name="com.rong.web.action" namespace="/" extends="struts-default">
        <action name="search" class="com.ronng.web.action.OneAction">
            <result>/index.jsp</result>
        </action>
    </package>
    
</struts>

 5.获取域对象以及ActionContext上下文

A.第一种方式(通过ServletActionContext获取)

 1 package com.ronng.web.action;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpSession;
 6 import javax.servlet.jsp.PageContext;
 7 
 8 import org.apache.struts2.ServletActionContext;
 9 
10 /**
11  * 普通类获取域对象
12  * 通过ServletActionContext获取
13  * @author Rong
14  *
15  */
16 public class TwoAction {
17     public String show() throws Exception{
18         //使用原生API,耦合度高,很少使用
19         PageContext pageContext = ServletActionContext.getPageContext();
20         //可通过pageContext获取其余域对象
21 //        ServletRequest request = pageContext.getRequest();
22 //        HttpSession session = pageContext.getSession();
23 //        ServletContext application = pageContext.getServletContext();
24         HttpServletRequest request = ServletActionContext.getRequest();
25         //获取前台传过来的值
26         String parameter = request.getParameter("name");
27         System.out.println(parameter);
28         HttpSession session = request.getSession();
29         ServletContext application = ServletActionContext.getServletContext();
30         request.setAttribute("name", "rong");
31         session.setAttribute("name", "jie");
32         application.setAttribute("name", "long");
33         return "success";
34     }
35 }
<body>
    通用的域: ${name }<br/>
    request域:${requestScope.name }<br/>
    session域:${sessionScope.name }<br/>
    application域:${applicationScope.name }<br/>
</body>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="com.rong.web.action" namespace="/" extends="struts-default">
        <action name="search" class="com.ronng.web.action.TwoAction" method="show">
            <result>/two.jsp</result>
        </action>
    </package>
</struts>

B.第二种方式(ActionContext上下文的get方式)

 1 package com.ronng.web.action;
 2 
 3 import org.apache.struts2.dispatcher.ApplicationMap;
 4 import org.apache.struts2.dispatcher.RequestMap;
 5 import org.apache.struts2.dispatcher.SessionMap;
 6 
 7 import com.opensymphony.xwork2.ActionContext;
 8 
 9 /**
10  * 普通类获取域对象
11  * 通过ActionContext上下文get方式获取
12  * 松耦合
13  * @author Rong
14  */
15 public class TwoAction {
16     public String show() throws Exception{
17         //获取ActionContext上下文。ActionContext在请求最开始的时候封装了所有数据。
18         ActionContext actionContext = ActionContext.getContext();
19         //通过get方法获取域对象。其中get里面的字符串是唯一指定的。
20         RequestMap requestMap = (RequestMap) actionContext.get("request");
21         SessionMap<String, Object> sessionMap = (SessionMap<String, Object>) actionContext.get("session");
22         ApplicationMap applicationMap = (ApplicationMap) actionContext.get("application");
23         requestMap.put("name", "rong");
24         sessionMap.put("name", "jie");
25         applicationMap.put("name", "long");
26         return "success";
27     }
28 }

C.第三种方式(ActionContext上下文的getXxx方式)

 1 package com.ronng.web.action;
 2 
 3 import java.util.Map;
 4 
 5 import com.opensymphony.xwork2.ActionContext;
 6 
 7 /**
 8  * 普通类获取域对象
 9  * 通过ActionContext上下文getXxx方式获取
10  * 松耦合
11  * @author Rong
12  */
13 public class TwoAction {
14     public String show() throws Exception{
15         //获取ActionContext上下文。ActionContext在请求最开始的时候封装了所有数据。
16         ActionContext actionContext = ActionContext.getContext();
17         Map<String, Object> request = actionContext.getContextMap();
18         Map<String, Object> session = actionContext.getSession();
19         Map<String, Object> application = actionContext.getApplication();
20         request.put("name", "rong");
21         session.put("name", "jie");
22         application.put("name", "long");
23         return "success";
24     }
25 }

D.第四种方式(实现XxxAware接口)

根据需要实现不同域的接口

 1 package com.ronng.web.action;
 2 
 3 import java.util.Map;
 4 
 5 import org.apache.struts2.interceptor.ApplicationAware;
 6 import org.apache.struts2.interceptor.RequestAware;
 7 import org.apache.struts2.interceptor.SessionAware;
 8 
 9 
10 /**
11  * 实现接口方式获取域对象
12  * 松耦合
13  * @author Rong
14  * 这里列举了实现多种域接口,实际根据自己需要实现接口
15  */
16 public class TwoAction implements RequestAware,SessionAware,ApplicationAware{
17     //需要自己去声明成员变量(域对象)
18     private Map<String, Object> request;
19     private Map<String, Object> session;
20     private Map<String, Object> applicaton;
21     public String show() throws Exception{
22         request.put("name", "r");
23         session.put("name", "j");
24         applicaton.put("name", "l");
25         return "success";
26     }
27     //域对象赋值
28     @Override
29     public void setApplication(Map<String, Object> arg0) {
30         this.applicaton=arg0;
31     }
32 
33     @Override
34     public void setSession(Map<String, Object> arg0) {
35         this.session=arg0;
36     }
37 
38     @Override
39     public void setRequest(Map<String, Object> arg0) {
40         this.request=arg0;
41     }
42 }

 

以上是关于Struts2的主要内容,如果未能解决你的问题,请参考以下文章

struts2怎么防止sql注入

struts2请求过程源代码分析

[struts2学习笔记] 第五节 编写struts2的action代码

Struts2 s2-032远程代码执行分析

S2-053:Apache Struts2远程代码执行漏洞(中危)

struts2 s2-062 ONGL远程代码执行