Struts2之2.5.10.1HelloWorld

  Struts2.5.10.1是目前为止最新的版本,struts2建议持续跟进,理由大家都懂。好了,下面步入正题。

  基于struts2.5.10.1建立一个HelloWorld,基于注解的哈!

  工具:eclipse neon3,jdk8,tomcat8,windows10(无关紧要)

  需要的jar包:struts2官网下载

  (1)struts-2.5.10.1-min-lib.zip:用到解压后lib文件夹下的所有jar包;

  (2)struts-2.5.10.1-all.zip:struts2-convention-plugin-2.5.10.1.jar(因为要用到注解)。

  eclipse新建一个动态web工程HelloStruts2,拷贝上述中提到的jar包到工程的lib文件加下,然后引入到工程中;配置web.xml

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5     id="WebApp_ID" version="3.1">
 6     <display-name>HelloStruts2</display-name>
 7 
 8     <!-- 配置核心拦截器 -->
 9     <filter>
10         <!-- filter的名字 -->
11         <filter-name>struts2</filter-name>
12         <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
13         <!-- 设置struts2默认编码集为UTF-8 -->
14         <init-param>
15             <param-name>struts.il8.encoding</param-name>
16             <param-value>UTF-8</param-value>
17         </init-param>
18         <!-- 设置struts.xml文件位置 -->
19         <init-param>
20             <param-name>filterConfig</param-name>
21             <param-value>classpath:struts.xml</param-value>
22         </init-param>
23     </filter>
24     <filter-mapping>
25         <filter-name>struts2</filter-name>
26         <!-- 拦截所有的URL -->
27         <url-pattern>/*</url-pattern>
28     </filter-mapping>
29 
30     <welcome-file-list>
31         <welcome-file>index.html</welcome-file>
32         <welcome-file>index.htm</welcome-file>
33         <welcome-file>index.jsp</welcome-file>
34         <welcome-file>default.html</welcome-file>
35         <welcome-file>default.htm</welcome-file>
36         <welcome-file>default.jsp</welcome-file>
37     </welcome-file-list>
38 </web-app>
View Code

 

  在src下建立一个资源文件夹config,在其下建立struts.xml

  

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 4     "http://struts.apache.org/dtds/struts-2.5.dtd">
 5 
 6 <!-- START SNIPPET: xworkSample -->
 7 <struts>
 8 
 9     <!-- Some or all of these can be flipped to true for debugging -->
10     <!-- 设置默认编码集为UTF-8 -->
11     <constant name="struts.il8n.encoding" value="UTF-8" />
12     <!--设置主题 -->
13     <!-- <constant name="struts.ui.theme" value="simple" /> -->
14     <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
15     <constant name="struts.serve.static.browserCache" value="false" />
16     <constant name="struts.convention.default.parent.package"
17         value="default" />
18     <constant name="struts.i18n.reload" value="false" />
19     <!-- 设置是否支持动态方法调用,true为支持,false不支持. -->
20     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
21     <!--设置开发者模式 -->
22     <constant name="struts.devMode" value="false" />
23     <!-- 当 struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生 产环境下使用),开发阶段最好打开 -->
24     <constant name="struts.configuration.xml.reload" value="true" />
25     <!-- 该 属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即 所有匹配*.action的请求都由Struts 2处理。如 
26         果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开 -->
27     <constant name="struts.action.extension" value="action," />
28     <constant name="struts.convention.default.parent.package" value="struts-default"/>
29     <!-- <package name="default" namespace="/" extends="struts-default">
30         name action的名字,访问时使用helloworld.action访问,class:实现类
31         <action name="helloWorld" class="com.HelloStruts2.action.HelloWorldAction">
32             结果集,即action中SUCCESS返回的视图
33             <result>
34                 /result.jsp
35             </result>
36         </action>
37     </package> -->
38 </struts>
39 
40 <!-- END SNIPPET: xworkSample -->
View Code

 

  在src下建立包com.HelloStruts2.action,然后在action下新建class,HelloWorldAction:

  

 1 package com.HelloStruts2.action;
 2 
 3 import org.apache.struts2.convention.annotation.Action;
 4 import org.apache.struts2.convention.annotation.Namespace;
 5 import org.apache.struts2.convention.annotation.Result;
 6 import org.apache.struts2.convention.annotation.Results;
 7 
 8 import com.opensymphony.xwork2.ActionSupport;
 9 
10 /**
11  * @ClassName: HelloWorldAction
12  * @Description: HelloWorrldAction类
13  * @author 腾龙问天
14  * @date 2017年5月11日 下午8:34:42
15  * 
16  */
17 @Namespace("/")
18 @Results({@Result(name="SUCCESS", type="dispatcher", location="/result.jsp"),
19     @Result(name="hiWorld", type="dispatcher", location="/end.jsp")})
20 public class HelloWorldAction extends ActionSupport {
21 //    @Action(value = "helloWorld", results = {
22 //            @Result(name = "SUCCESS", type = "dispatcher", location = "/result.jsp") })
23     @Action(value="helloWorld")
24     public String execute() throws Exception {
25         System.out.println("正在执行的Action");
26         // 返回视图 SUCCESS,这是框架定义
27         return "SUCCESS";
28     }
29     @Action(value="hiWorld")
30     public String hiWorld(){
31         System.out.println("hi World");
32         return "hiWorld";
33     }
34 }
View Code

 

  工程的WebContent下新建两个jsp页面,result.jsp和end.jsp两个页面,然后随意输入点内容,具体内容你开心就好。

然后加载到tomcat运行。

  运行后如果浏览器中的路径为:http://localhost:8080/HelloStruts2/,你看到的可能是你不想看到的,但是别慌,后面加上result.jsp或者end.jsp即可看到效果,并且控制台可以看到对应的输出。不加后续路径404的原因是因为web.xml中是这样写的:

  

1 <welcome-file-list>
2         <welcome-file>index.html</welcome-file>
3         <welcome-file>index.htm</welcome-file>
4         <welcome-file>index.jsp</welcome-file>
5         <welcome-file>default.html</welcome-file>
6         <welcome-file>default.htm</welcome-file>
7         <welcome-file>default.jsp</welcome-file>
8     </welcome-file-list>
View Code

 

  不信邪的你,可以把result.jsp或者end.jsp其中一个加到其下再次运行试试看咯。

  下面说下注解(前提是引入struts2-convention-plugin-2.5.10.1.jar,不引入用不了注解,爱引不引):

  Namespace:Namespace注解用于指定Action所在的包的命名空间。该注解只有一个value参数,用于指定ACtion所属于的命名空间。

  当使用Namespace注解时,在为命名空间取名需要使用斜杠(/)开头,例如这样

  

1 @Namespace("/user")  
2 public class UserAction extends ActionSupport{  
3      
4 }
View Code

 

  通俗点:可以理解为“路径的概念”,我这里写“/”,因为懒;

  Result:Result注解用于定义一个Result映射,该注解包含四个参数,

  1)name:可选参数,用于指定Result的逻辑名,默认值为success

  2)location:必选参数,用于指定Result对应资源的URL

  3)type:可选参数,用于指定Result的类型,默认值为NullResult.class

  4)params:可选参数,用于为Result指定要传递的参数,格式为:{key1,value1,key2,value2,...}

  

1 @Result(name="login",location="/login.jsp",params={},type="dispatcher")  
2 public class UserAction extends ActionSupport{  
3      
4 }
View Code

 

  Action:Action注解对应于struts.xml文件中的action元素。该注解可用于Action类上,也可用于方法上。这个注解包含一下的几个属性:

  1)value:可选参数,表示Action的名字

  2)results:可选参数,表示Action的多个Result映射。该属性用于定义一组Result映射

  3)interceptorRefs:可选参数,表示Action的多个拦截器。该属性用于定义一组拦截器

  4)params:可选参数,表示传递给Action的参数,格式为{key1,value1,key2,value2,...}

  5)exceptionMappings:可选参数,指定Action的异常处理类,他是一个Exception-Mapping的数组属性

  

 1 @Action{  
 2    value="user",  
 3    interceptorRefs = {  
 4        @InterceptorRefs(value="fileUpload",params={"maximumSize","1024000","allowedTypes",image/123}),  
 5        @InterceptorRefs(value = "basicStack")  
 6    },  
 7    results = {  
 8       @Result(name="success",location="success.jsp"),  
 9       @Result(name="login",location="login.jsp")  
10    },  
11    exceptionMappings = {  
12        @ExceptionMapping(exception="java.lang.Exception",result="error")  
13    }  
14 }  
15 public class UserAction extends ActionSupport{  
16      
17 }
View Code

 

  上面讲解的写法和上上面举例的那个写法都没错哦,只是两种方式,我比较倾向于我上上面举例的写法。

  贴下运行结果图:

  

  

  title都是result,别误会,没毛病,只是两个页面的title都是result而已。

  对应的控制台输出:

  

  转载请注明出处,谢谢,未经同意转载方请不要随意加水印,理由:影响阅读。

  

posted @ 2017-05-11 22:10  腾龙问天  阅读(764)  评论(0编辑  收藏  举报