Struts 2.3.4.1完整示例

【系统环境】Windows 7 Ultimate 64 Bit

【开发环境】JDK1.6.21,Tomcat6.0.35,MyEclipse10

【其他环境】Struts2.3.4.1

【项目描述】Struts2.3.4.1完整示例

目录结构:

Struts 2.3.4.1完整示例

使用的jar包

Struts 2.3.4.1完整示例

struts.xml 文件内容:

1 <?xml
version="1.0" encoding="UTF-8" ?>

2 <!DOCTYPE struts PUBLIC

3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

4     "http://struts.apache.org/dtds/struts-2.3.dtd">

5

6 <struts>

7     <package name="struts2demo" extends="struts-default">

8         <action name="hello"

9             class="zain.struts2.action.HelloWorldAction">

10             <result name="success">/jsp/success.jsp</result>

11             <result name="failure">/jsp/failure.jsp</result>

12         </action>

13     </package>

14 </struts>
 
上面代码中的 package节点中,name属性名可以随意取,它的功能是可以实现继承,还有namespace属性名是防止重名,比如项目中不同的包下,有同名Action,添加namespace属性值之后,可以通过命名空间名.Action名称来区分;因为Struts支持多个配置文件,所以exntends属性是用来指定此文件继承自默认配置文件。

我们知道,当我们引入Struts支持的时候,所有的Web请求都将交给Struts核心控制器来处理,根据不同的Action来执行不同的响应,这里的Action的name属性,即是当我们调用这个Action时,执行对应的ActionClass。
 

web.xml 文件内容

1 <?xml
version="1.0" encoding="UTF-8"?>

2 <web-app version="3.0"

3     xmlns="http://java.sun.com/xml/ns/javaee"

4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

7   <display-name></display-name>   

8  

9   <!-- 定义Struts2.3.4过滤器 -->

10   <filter>

11       <filter-name>struts2</filter-name>

12       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

13   </filter>

14  

15   <!-- Struts2.3.4过滤器映射 -->

16   <filter-mapping>

17       <filter-name>struts2</filter-name>

18       <url-pattern>/*</url-pattern>

19   </filter-mapping>

20  

21   <welcome-file-list>

22     <welcome-file>index.jsp</welcome-file>

23   </welcome-file-list>

24 </web-app>
 
HelloWorldAction.java 文件内容
1 package zain.struts2.action;

2

3 public class HelloWorldAction
{

4    

5     private String
message;

6    

7     public String
getMessage(){

8         return message;

9     }

10    

11     public String
execute(){

12         message = "Struts2.3.4演示程序。";

13         return "success";

14     }

15

16 }


启动Web服务器并将项目发布完成之后,我们可以执行如下请求来测试是否正确:

http://localhost:8080/Struts234Demo/hello.action (推荐,简洁的不一定是最好的,但一定是最容易理解的)

http://localhost:8080/Struts234Demo/hello!execute.action(如果Action没有重写execute方法,我们也可以通过指定其他方法来处理请求)

http://localhost:8080/Struts234Demo/hello.action?method=execute(如果没有指定请求的方法,那么默认调用execute方法,所以'?method=execute'是可以省略的)

由这里,我们又能发现一个问题:我能否使用.do来进行请求?为什么会出现404?

为什么我在配置web.xml文件时,<url-pattern>里是 /*,却不能使用.do结尾来请求?

这里,我们需要关心一下Struts2的另外一个文件:struts.properties

在其中我们配置:<constant name="struts.action.extension" value="action,do[,x]"/> 注[]为可选内容,可以是一个列表,我们也可以将其配置到struts.xml中

另外,我们可以配置:struts.i18n.encoding=UTF-8 来指定Web应用程序所使用的字符集,处理中文请求的时候多有用到。

上一篇:android玩耍(-) adbshell安装


下一篇:Android开发之 Windows环境下通过Eclipse创建的第一个安卓应用程序(图文详细步骤)