Struts2 初体验

   Sturts是一款优雅的,可扩展性很强的框架。它是由Struts开发团队和WebWord团队合作,共同开发的一个新的产品。新版本的Struts2 更加容易使用,更加接近Struts所追求的理念。从开发,部署么维护中效率的提高,无疑会让其能够继续谱写Struts1的辉煌,面对如些大的诱惑,让我们一起走入Struts2的世界吧!

下面的小例子演示了Struts2的简单配置:

一、Struts2资源下载。

官网地址:http://www.apache.org/

下载解压后能看到如下图的内容:

apps:官方提供的Struts2应用示例

docs:Struts2 文档

lib:Struts2 发行包及依赖包

src:Struts2 源代码

其余部分是Struts2 及其依赖包的使用许可协议和声明

二、添加Struts2至项目

新建java wed项目。选择Struts2 资源中lib下 struts2-core-2.3.15.1.jar,xwork-core-2.3.15.1.jar,ognl-3.0.6.jar,freemarker-2.3.19.jar,commons-fileupload-1.3.jar五个jar文件,添加到项目。把jar包复制到项目下webContent\web-inf\lib文件夹中即可。

注意:不同版本的struts2所需要添加的包也有所不同,这里建议把Struts2 资源中apps目录下的struts2-blank.war项目解压。打开解压后的文件夹。把\web-inf\lib下的所有jar包添加至项目。如下图:

三、创建测试用的JSP页面hellostruts2.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>测试Struts2</title>
 8 </head>
 9 <body>
10     ${message }
11     <!--注意:表单提交的路径以.action结尾-->
12     <form action="hello.action">
13         <input type="text" name="name" />
14         <input type="submit" value="提交" />
15     </form>
16 </body>
17 </html>

四、创建HelloAction类,用于对用户的请求做出处理。

 1 public class HelloAction {
 2     /*
 3      * 对象的属性名要和页面传过来的参数名一致,并提供相应的get,set方法。
 4      * Struts2会把页面传来的参数值赋值给名字相同的属性。
 5      * 并默认把属性存在Request范围里。
 6      */
 7     private String name;
 8     private String message;
 9     
10     /*
11      * 方法需要返回字符串类型。配置完成后Struts2会把页面转到和该字符串对应的页面上。
12      */
13     public String hello(){
14         //为message赋值,message同样也会被传到request范围里
15         message="hello"+name;
16         return "hellostruts2";
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public String getMessage() {
28         return message;
29     }
30 
31     public void setMessage(String message) {
32         this.message = message;
33     }    
34 }

五、修改项目的web.xml配置文件。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
 3   <display-name>HelloStruts</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13   <!--添加如下过滤器配置,将全部请求定位到Strust2 过滤器中。-->
14   <filter>
15         <filter-name>struts2</filter-name>
16         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
17     </filter>
18     
19     <filter-mapping>
20         <filter-name>struts2</filter-name>
21         <url-pattern>/*</url-pattern>
22     </filter-mapping>
23   
24 </web-app>

六、在src目录下创建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 
 8     <!--添加 package 标签,name=default,extends=struts-default-->
 9     <package name="default" namespace="/" extends="struts-default">
10 
11         <!--添加 action 标签,name为表单提交的路径,后面不要加.action。-->
12         <!--class表示要跳到哪个类里执行-->
13         <!--method表示要执行类里哪个方法,默认执行execute方法-->
14         <action name="hello" class="HelloAction" method="hello">
15             <!--结果为hellostruts2 时,跳转至hellostruts2.jsp页面-->
16             <result name="hellostruts2">hellostruts2.jsp</result>
17         </action>
18         
19     </package>
20 
21 
22 </struts>

最后一步:编译部署并启动服务器,访问hellostruts2.jsp

提交表单后:

 

 

转载于:https://www.cnblogs.com/likailan/p/3280820.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值