struts2的简介

struts2在webwork2基础发展而来,struts2与struts1相比,没有跟ServletAPI和struts API有着紧密的耦合,struts2为无侵入式设计,而struts1却属于侵入式设计;struts2提供了拦截器,利用拦截器可以进行AOP编程,实现如权限拦截等功能;struts2提供了类型转换器,struts1的数据类型转换是通过BeanUtil来实现的。

struts2在StrutsPrepareAndExcuteFilter的init()方法中将读取类路径下默认的配置文件struts.xml完成初始化操作,读取struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件。

struts2的基本配置:

web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
      <filter-name>helloAction</filter-name>
      <filter-class>control.center.HelloWordAction</filter-class>
  </filter>
  <filter-mapping>
     <filter-name>helloAction</filter-name>
     <url-pattern>/test</url-pattern>
  </filter-mapping>
</web-app>

struts-config.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>
    <!--
                     在struts2中将action放在包中
        namespace:指的是action的路径
        struts-default包:定义了拦截器和result类型,继承了该包才能使用struts2的核心功能
    -->
    <package name="default" namespace="/control/center" extends="struts-default">
        <action name="index">
            <!-- result相当于ActionForward -->
            <result name="success">/WEB-INF/pages/hello.jsp</result>
        </action>
    </package>

    <!-- Add packages here -->

</struts>

1,如果没有为aciton指定class,那么该类为ActionSupport类

2,如果没有为action指定method方法,那么默认执行execute方法

3,如果没有指定result的name属性,默认值为success

Action:

package control.center;

public class HelloWordAction {

 private String message;
 
 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }

 public String execute(){
  this.message="我的第一个struts应用";

   return "success";
 }
 
}

Action的收索顺序,首先查找此action前的命名空间包是否存在,如果不存在就查找上层目录的命名空间包,如果不存在则在查找上层目录的命名空间包,如果在包下查找不到action,那么就会到默认命名空间中去查找action。

重定向不能直接访问web-inf目录:

struts2的重定向:

 <action name="redirect">
            <!-- 重定向 -->
            <result type="redirect">/hello.jsp?username=${username}</result>
        </action>

其中${username}类似el表达式,可以取出javabean中的username属性。

result默认的是dispatcher在服务器端一次请求

URL编码:

URLEncoder.encode("星期三","UTF-8");

URL解码:

URLEncoder.decode(new String(request.getParameter("username").getBytes("ISO8859"),"UTF-8"),"UTF-8")

重定向到某个Action:

        <action name="index">
            <!-- result相当于ActionForward -->
            <result name="success">/WEB-INF/pages/hello.jsp</result>
        </action>
       
        <action name="redirect">
            <!-- 重定向到名为index的action -->
            <result type="redirectAction">index</result>
        </action>

重定向到某个包中的action:

        <action name="redirect">
            <!-- 重定向到名为index的action -->
            <result type="redirectAction">

                 <!-- 将参数注入到类型对应的java类中 -->

                 <param  name="actionName">action的名称</param>

                <param   name="namespace">action的命名空间</param>

           </result>
        </action>

包:

<package name="default" namespace="/control/center" extends="struts-default">
        <action name="index">
            <!-- result相当于ActionForward -->
            <result name="success">/WEB-INF/pages/hello.jsp</result>
        </action>
       
        <action name="redirect">
            <!-- 重定向 -->
            <result type="redirect">/hello.jsp?username=${username}</result>
        </action>
       
    </package>

result的类型plainText表示原样输入页面代码:

<action name="redirect">
            <!-- 重定向 -->
            <result type="plaintText">

                      <param name="location">指定请求路径</param>

                       <!-- 指定文件的读取路径-->

                     <param name="ObacSet">UTF-8</param>

           </result>

</action>

使用<global-result></global-result>定义全局跳转视图

原文地址:https://www.cnblogs.com/jinzhengquan/p/1962645.html