Struts2笔记1

1.  struts2是在WebWork2基础上发展来的,和Struts1一样,struts2也属于MVC框架

2.  struts2有以下优点:

  1)不跟ServletAPI和StrutsAPI有紧密的耦合,属于无侵入式设计

  2)struts2提供了拦截器,可以进行AOP编程

  3)提供了类型转换器,我们可以把特殊的请求参数转换为需要的类型

  4)提供了多种表现层技术,如JSP、freeMarker、velocity

  5)可以对指定方法进行校验

  6)提供了全局范围、包范围和Action范围的国际化资源文件管理

3.  开发Struts2应用的最少jar文件:

  struts2-core-2.1.8.jar------struts2框架的核心类库

  xwork-core-2.1.6.jar------xwork核心类库,struts2在其上构建

  ognl-2.7.3.jar-----对象图导航语言,Struts2通过其读写对象的属性

  freemarker-2.3.15.jar-----struts2的UI标签的模板使用FreeMarker编写

  commons-logging-1.0.4.jar-----struts2使用这个日志包来支持log4j和jdk1.4+的日志记录

  commons-fileupload-1.2.1.jar-----文件上传组件,2.1.6版本后必须加入此文件

4.  将apps下的例子解压,如struts2-blank-2.1.8,在WEB-INF\classes\struts.xml中找xml配置模板

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4     "http://struts.apache.org/dtds/struts-2.0.dtd">
5 
6 <struts>
7 </struts>

5.  在web.xml中配置filter,struts2通过Filter启动

 1 <filter>
 2         <filter-name>struts</filter-name>
 3         <filter-class>
 4             org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter
 5         </filter-class>
 6 </filter>
 7 <filter-mapping>
 8         <filter-name>struts</filter-name>
 9         <url-pattern>/*</url-pattern>
10  </filter-mapping>

注意:struts2读取到struts.xml中的内容后,以javabean的形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取xml文件
6.  第一个struts2应用,新建web项目如工程名为:struts2

  1)在src目录下新建struts.xml,配置action

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
3 <struts>
4     <package name="itcast" namespace="/test" extends="struts-default" >
5         <action name="orgList" class="cn.itcast.action.helloworld"  method="execute">
6             <result name="success">/WEB-INF/pages/helloworld.jsp</result>
7         </action>
8      </package>
9 </struts>

ps:包的作用与java中包的作用类似,主要用于管理组业务功能相关的action。name属性可以任意但必须唯一,用于继承。namespace定义命名空间作为访问action的路径一部分,如上面的访问路径为/struts2/test/orgList,默认为“”。通常每个包都应继承“struts-default” 包,才能使用struts2提供的核心功能。

包还可以通过abstract=“true”定义为抽象包,抽象包中不能包含action

  2)创建在xml中定义的action,方法名为method属性定义的名字,返回值为String类型,即返回视图的名称(result标签中定义的name)

 1 package cn.itcast.action;
 2 
 3 public class helloworld {
 4     private String msg;
 5     
 6     public String getMessage() {
 7         return msg;
 8     }
 9 
10     public String execute(){
11         msg = "第一个struts应用";
12         return "success";
13     }
14 }

  3)创建页面,在页面中取到action中用${message}定义的属性,message为action中getMessage方法中去掉get后首字母小写

 1 <%@ page language="java" pageEncoding="utf-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 6 <title>Insert title here</title>
 7 </head>
 8 <body>
 9 ${message}
10 </body>
11 </html>

7.  Action名称的搜索顺序:
  如请求的URL是:http://server/struts2/path1/path2/path3/test.action,首先寻找namespace为/path1/path2/path3的package,如果存在,就在这个package中找名称为test的action,如果不存在,就在默认namespace的package中寻找名称为test的action,如果找不到就找namespace为/path1/path2的package,如果存在。。。。。。直到找到namespace为/path1的package,如果存在,就在这个package中找名称为test的action,如果不存在,就在默认namespace的package中寻找名称为test的action。

  默认的后缀是.action,URL中不写后缀也能访问到。

8.  Action配置中各项默认值:

  1)如果没有为action指定class,默认为“ActionSupport”(函数返回一个“success”)

  2)如果没有为action指定method。默认为“execute”

  3)如果没有指令result的name属性,那么默认为“success”

    <action name="hello">
            <result>/WEB-INF/pages/helloworld.jsp</result>
        </action>

  所以这个action会直接请求到jsp页面
9.  Action中的result的各种转发类型

  1)type为“redirect”表示重定向,请求地址为http://localhost:8080/struts2/test/redirection,会直接转发到http://localhost:8080/struts2/user.jsp,这个jsp应该是用户能直接访问的,不在WEB-INF目录下的页面(地址栏会发生变化)

<action name="redirection">
            <result type="redirect">/user.jsp</result>
        </action>

  2)如果要在请求转发的时候带参数,比如跳到修改界面要带上信息如http://localhost:8080/struts2/user.jsp&msg="liming",要在action中加这个属性,并且加上getName方法,在execute方法中赋值即可,同上。就可以再action中配置?msg=${message}

package cn.itcast.action;

public class helloworld {
    private String msg;
    
    public String getMessage() {
        return msg;
    }

    public String execute(){
        msg = "liming";
        return "success";
    }
}
<action name="orgList" class="cn.itcast.action.helloworld"  method="execute">
            <result type="redirect">/user.jsp?msg=${message}</result>
        </action>

  页面用${param.msg}得到请求传递过来的参数msg的值。

  3)如果要往前台传的值是汉字,如msg=“第一个”;那么 execute中代码为:

public String execute() throws Exception{
        //msg="liming";
        msg = URLEncoder.encode("第一个","UTF-8");
        return "success";
    }

前台为:

    <%= URLDecoder.decode(new String(request.getParameter("message").getBytes("ISO8859-1"),"UTF-8"),"UTF-8") %>

  4)type="redirectAction"表示重定向到action,将actionName传进去

<action name="redirectAction">
            <result type="redirectAction">orgList</result>
        </action>

    如果不在同一个包下,用param传入actionName和namespace:

  <package name="itcast" namespace="/test" extends="struts-default" >
     <action name="redirectAction">
            <result type="redirectAction">
                <param name="actionName">xxx</param>
                <param name="namespace">/test2</param>
            </result>
        </action>
     </package>
     <package name="other" namespace="/test2" extends="struts-default">
         <action name="xxx">
             <result>/user.jsp</result>
         </action>
     </package>

  5)type=“plainText”表示原样输出页面源代码,主要用于技术网站显示源代码

    <action name="plainText">
            <result type="plainText">/user.jsp</result>
        </action>

  若页面中有中文会存在乱码,解决方法:如果页面的编码为utf-8,那么需要指定符集参数

<action name="plainText">
            <result type="plainText">
                <param name="location">/index.jsp</param>
                <param name="charSet">utf-8</param>
            </result>
        </action>

  6)可以再包下设置全局的视图result供包下的action调用

    <global-results >
             <result name="message">/index.jsp</result>
        </global-results>

  也可以调用其他包下的视图,但是必须继承那个包,在execute方法中返回全局视图的名称

  <package name="base" namespace="/test2" extends="struts-default">
         <global-results >
             <result name="message">/index.jsp</result>
        </global-results>
     </package>
    <package name="itcast" namespace="/test" extends="base" >
        <action name="orgList" class="cn.itcast.action.helloworld"  method="execute">
        </action>
     </package>
原文地址:https://www.cnblogs.com/fanglove/p/2832874.html