这个例子讲解的是Struts2的Hello 例子。
本文的整合的环境是:
1.Maven 3
2.Eclipse 3.7
3.Struts 2.3.1.2
1.项目结构图:
本文的最终结构图如下所示,防止你跟不上后面的步骤
2.添加Struts2依赖
使用Maven需要添加Struts2依赖,在pom.xml文件里需要添加“struts2-core”
pom.xml文件内容如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < groupId >com.mkyong.common</ groupId > < artifactId >Struts2Example</ artifactId > < packaging >war</ packaging > < version >com.mkyong.common</ version > < name >Struts2Example Maven Webapp</ name > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >3.8.1</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.apache.struts</ groupId > < artifactId >struts2-core</ artifactId > < version >2.3.1.2</ version > </ dependency > </ dependencies > < build > < finalName >Struts2Example</ finalName > < plugins > < plugin > < artifactId >maven-compiler-plugin</ artifactId > < version >2.3.2</ version > < configuration > < source >1.6</ source > < target >1.6</ target > </ configuration > </ plugin > </ plugins > </ build > </ project > |
3.转换到Eclipse项目
编译并转换到Eclipse Web项目需要执行如下命令提示符:
1
|
mvn eclipse:eclipse -Dwtpversion=2.0 |
复查Eclipse的classpath文件,下面的Struts2的依赖关系被下载:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< classpath > < classpathentry kind = "src" path = "src/main/java" including = "**/*.java" /> < classpathentry kind = "src" path = "src/main/resources" excluding = "**/*.java" /> < classpathentry kind = "output" path = "target/classes" /> < classpathentry kind = "var" path = "M2_REPO/asm/asm/3.3/asm-3.3.jar" /> < classpathentry kind = "var" path = "M2_REPO/asm/asm-commons/3.3/asm-commons-3.3.jar" /> < classpathentry kind = "var" path = "M2_REPO/asm/asm-tree/3.3/asm-tree-3.3.jar" /> < classpathentry kind = "var" path = "M2_REPO/commons-fileupload/commons-fileupload/1.2.2/commons-fileupload-1.2.2.jar" /> < classpathentry kind = "var" path = "M2_REPO/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar" /> < classpathentry kind = "var" path = "M2_REPO/commons-lang/commons-lang/2.5/commons-lang-2.5.jar" /> < classpathentry kind = "var" path = "M2_REPO/org/freemarker/freemarker/2.3.18/freemarker-2.3.18.jar" /> < classpathentry kind = "var" path = "M2_REPO/javassist/javassist/3.11.0.GA/javassist-3.11.0.GA.jar" /> < classpathentry kind = "var" path = "M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" /> < classpathentry kind = "var" path = "M2_REPO/ognl/ognl/3.0.4/ognl-3.0.4.jar" /> < classpathentry kind = "var" path = "M2_REPO/org/apache/struts/struts2-core/2.3.1.2/struts2-core-2.3.1.2.jar" /> < classpathentry kind = "lib" path = "C:/Program Files/Java/jdk1.6.0_13/lib/tools.jar" /> < classpathentry kind = "var" path = "M2_REPO/org/apache/struts/xwork/xwork-core/2.3.1.2/xwork-core-2.3.1.2.jar" /> < classpathentry kind = "con" path = "org.eclipse.jdt.launching.JRE_CONTAINER" /> </ classpath > |
4.JSP页面
下面的登陆页面用的是Struts2标签,里面包含一个用户名,密码和一个提交按钮
login.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> < html > < head ></ head > < body > < h1 >Struts 2 Hello World Example</ h1 > < s:form action = "Welcome" > < s:textfield name = "username" label = "Username" /> < s:password name = "password" label = "Password" /> < s:submit /> </ s:form > </ body > </ html > |
welcome_user.jsp ,这个页面是给用户展示欢迎信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> < html > < head ></ head > < body > < h1 >Struts 2 Hello World Example</ h1 > < h4 > Hello < s:property value = "username" /> </ h4 > </ body > </ html > |
Struts1和Struts2的标签语法非常相似,仅仅是标签的命名上有点小区别,例如:
Struts1如下所示:
1
2
3
4
|
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> < html:form action = "Welcome" > < html:text property = "username" /> </ html:form > |
Struts2如下所示:
1
2
3
|
<%@ taglib prefix="s" uri="/struts-tags" %> < s:form action = "Welcome" > < s:textfield name = "username" label = "Username" /> |
5.Action-把所有的业务逻辑放在这儿
这是一个简单的Struts2 Action类,这里用来处理所有的业务逻辑。
WelcomeUserAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.mkyong.user.action; public class WelcomeUserAction{ private String username; public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } // all struts logic here public String execute() { return "SUCCESS" ; } } |
在Struts2中,需要实现任何接口或者任何父类,但是它需要创建一个execute()方法用来处理所有的业务逻辑并返回一个String类型的值用来告诉用户跳转到哪儿去。
注意1:你也许看到有些开发者实现了com.opensymphony.xwork2.Action类,但是它不是必须需要实现的类,因为这个类只是提供了几个常量而已
注意2:Struts1是必须实现com.opensymphony.xwork2.Action类的,但是Struts2是可选的,你也可以实现com.opensymphony.xwork2.Action类去使用一些常量或者你可以继承com.opensymphony.xwork2.ActionSupport去使用某些默认的通用的值
5.Struts2配置文件
Struts2的配置文件用来配置把所有的东西组装在一起,这个xml文件名必须为“struts.xml”
Struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" < struts > < package name = "user" namespace = "/User" extends = "struts-default" > < action name = "Login" > < result >pages/login.jsp</ result > </ action > < action name = "Welcome" class = "com.mkyong.user.action.WelcomeUserAction" > < result name = "SUCCESS" >pages/welcome_user.jsp</ result > </ action > </ package > </ struts > |
这里面配置了包名和Action类名,Action类名你也许很熟悉,但你也许会对下面的新标签感兴趣:
1.包名name=”user”
只是一个包名而已,不用太在意
2.命名空间namespace=”/User”
它用来匹配“/User”路径
3.继承包名 extends=”struts-default”
它的意思是集成struts-default的组件和拦截器,它声明在struts-default.xml文件里,这个文件存放在struts2-core.jar 文件里
6.Web.xml文件
在你的项目的web.xml文件里配置Struts2
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" < web-app > < display-name >Struts 2 Web Application</ display-name > < filter > < filter-name >struts2</ filter-name > < filter-class > org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </ filter-class > </ filter > < filter-mapping > < filter-name >struts2</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > </ web-app > |
7.运行
在struts2里,访问这个Action类你可以添加“.action”扩展名
ttp://localhost:8080/Struts2Example/User/Login.action
http://localhost:8080/Struts2Example/User/Welcome.action
原创文章,转载请注明出处:http://www.it161.com/article/javaDetail?articleid=140112224129
更多原创内容,请访问:http://www.it161.com/