构建后目录结果如下:
struts2 和struts1有很大的不同,它的核心特征是所有的实现都是通过interceptors(拦截器),“value stack” (值栈),OGNL(表达式语言),以及包含了很多的注解使框架使用起来更简单。
下面一步一步构建学习Struts2。
工具:
1: Maven Apache Maven 3.0.4
2: JDK 1.7.0_10
3: Eclipse4.2
4: apache-tomcat-6.0.35
首先通过Maven构建一个Web项目。
Maven构建web项目的命令如下:
-
mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
然后构建我们的项目,首先进入到项目放置的位置:
然后执行构建命令:如下:
-
E:\struts2>mvn archetype:generate -DgroupId=com.topwqp -DartifactId=Struts2Examp
-
le -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
-
[INFO] Scanning for projects...
-
[INFO]
-
[INFO] ------------------------------------------------------------------------
-
[INFO] Building Maven Stub Project (No POM) 1
-
[INFO] ------------------------------------------------------------------------
-
[INFO]
-
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>
-
>
-
[INFO]
-
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<
-
<
-
[INFO]
-
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --
-
-
-
[INFO] Generating project in Batch mode
-
[INFO] -------------------------------------------------------------------------
-
---
-
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
-
maven-archetype-webapp:1.0
-
[INFO] -------------------------------------------------------------------------
-
---
-
[INFO] Parameter: groupId, Value: com.topwqp
-
[INFO] Parameter: packageName, Value: com.topwqp
-
[INFO] Parameter: package, Value: com.topwqp
-
[INFO] Parameter: artifactId, Value: Struts2Example
-
[INFO] Parameter: basedir, Value: E:\struts2
-
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
-
[INFO] project created from Old (1.x) Archetype in dir: E:\struts2\Struts2Exampl
-
e
-
[INFO] ------------------------------------------------------------------------
-
[INFO] BUILD SUCCESS
-
[INFO] ------------------------------------------------------------------------
-
[INFO] Total time: 23.207s
-
[INFO] Finished at: Sat May 04 10:57:34 CST 2013
-
[INFO] Final Memory: 9M/24M
-
[INFO] ------------------------------------------------------------------------
-
E:\struts2>
构建成功后,生成的web项目的目录结构为:
|-src
|---main
|-----resources
|-----webapp
|-------index.jsp
|-------WEB-INF
|---------web.xml
|-pom.xml
然后把需要引入的jar包在pom.xml中进行配置。
配置后的pom.xml文件如下:
-
-
xsi:schemaLocation=" http://maven.apache.org/maven-v4_0_0.xsd">
-
4.0.0
-
com.topwqp
-
Struts2Example
-
war
-
1.0-SNAPSHOT
-
Struts2Example Maven Webapp
-
-
-
-
junit
-
junit
-
3.8.1
-
test
-
-
-
junit
-
junit
-
3.8.1
-
test
-
-
-
org.apache.struts
-
struts2-core
-
2.3.1.2
-
-
-
-
javassist
-
javassist
-
3.12.1.GA
-
-
-
-
-
Struts2Example
-
-
-
maven-compiler-plugin
-
2.3.2
-
-
-
1.7
-
-
-
-
-
配置pom.xml后,下一步需要导入到eclipse中,如果导入到eclispe中需要对项目进行转换,
转换为eclipse项目的命令 mvn eclipse:eclipse -Dwtpversion=2.0
如果运行以上命令首先需要进入到pom.xml的文件目录下:
运行结果如下:
-
E:\struts2>cd Struts2Example
-
-
E:\struts2\Struts2Example>mvn eclipse:eclipse -Dwtpversion=2.0
-
[INFO] Scanning for projects...
-
[INFO]
-
[INFO] ------------------------------------------------------------------------
-
[INFO] Building Struts2Example Maven Webapp 1.0-SNAPSHOT
-
[INFO] ------------------------------------------------------------------------
-
[INFO]
-
[INFO] >>> maven-eclipse-plugin:2.9:eclipse (default-cli) @ Struts2Example >>>
-
[INFO]
-
[INFO] <<< maven-eclipse-plugin:2.9:eclipse (default-cli) @ Struts2Example <<<
-
[INFO]
-
[INFO] --- maven-eclipse-plugin:2.9:eclipse (default-cli) @ Struts2Example ---
-
[INFO] Adding support for WTP version 2.0.
-
[INFO] Using Eclipse Workspace: null
-
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAIN
-
ER
-
Downloading:
-
truts2-core-2.3.1.2.pom
-
Downloaded:
-
ruts2-core-2.3.1.2.pom (12 KB at 4.2 KB/sec)
-
Downloading:
-
/struts2-parent-2.3.1.2.pom
-
Downloaded:
-
struts2-parent-2.3.1.2.pom (27 KB at 33.2 KB/sec)
-
Downloading:
-
-master-8.pom
-
Downloaded:
-
master-8.pom (9 KB at 20.9 KB/sec)
-
Downloading:
-
Downloaded: (14 KB a
-
t 32.6 KB/sec)
-
Downloading:
-
.2/xwork-core-2.3.1.2.pom
-
Downloaded:
-
2/xwork-core-2.3.1.2.pom (11 KB at 23.3 KB/sec)
-
Downloading:
-
Downloaded: (6 KB at 1
-
3.8 KB/sec)
-
[WARNING] Invalid POM for ognl:ognl:jar:3.0.4, transitive dependencies (if any)
-
will not be available, enable debug logging for more details
-
Downloading:
-
Downloaded: (264 B at 0.6 KB/
-
sec)
-
Downloading:
-
Downloaded: (5
-
KB at 9.9 KB/sec)
-
Downloading:
-
Downloaded: (
-
415 B at 1.0 KB/sec)
-
Downloading:
-
Downloaded: (404 B
-
at 1.0 KB/sec)
-
Downloading:
-
ker-2.3.18.pom
-
Downloaded:
-
er-2.3.18.pom (2 KB at 2.9 KB/sec)
-
Downloading:
-
truts2-core-2.3.1.2.jar
-
Downloaded:
-
ruts2-core-2.3.1.2.jar (757 KB at 100.6 KB/sec)
-
Downloading:
-
.2/xwork-core-2.3.1.2.jar
-
Downloaded:
-
2/xwork-core-2.3.1.2.jar (606 KB at 61.6 KB/sec)
-
Downloading:
-
Downloaded: (223 KB at
-
89.6 KB/sec)
-
Downloading:
-
Downloaded: (43 KB at 22.9 KB
-
/sec)
-
Downloading:
-
Downloaded: (
-
38 KB at 31.4 KB/sec)
-
Downloading:
-
Downloaded: (21 KB
-
at 35.6 KB/sec)
-
Downloading:
-
ker-2.3.18.jar
-
Downloaded:
-
er-2.3.18.jar (903 KB at 118.5 KB/sec)
-
Downloading:
-
.0.1.jar
-
Downloaded:
-
0.1.jar (156 KB at 81.0 KB/sec)
-
[INFO] Wrote settings to E:\struts2\Struts2Example\.settings\org.eclipse.jdt.cor
-
e.prefs
-
[INFO] Wrote Eclipse project for "Struts2Example" to E:\struts2\Struts2Example.
-
[INFO]
-
[INFO] ------------------------------------------------------------------------
-
[INFO] BUILD SUCCESS
-
[INFO] ------------------------------------------------------------------------
-
[INFO] Total time: 42.871s
-
[INFO] Finished at: Sat May 04 11:05:36 CST 2013
-
[INFO] Final Memory: 7M/19M
-
[INFO] ------------------------------------------------------------------------
-
E:\struts2\Struts2Example>
执行以上命令就是通过maven下载相应的依赖jar包,然后生成了一下文件:
.classpath
.project
.setting
以上三个文件夹是执行mvn eclipse:eclipse -Dwtpversion=2.0命令得到的。
这里有一个问题,现在没有弄明白,导入到eclipse中后发现没有src/main/java目录,打开生成的.classpath文件在第一行加上
然后在相应的目录下创建java的文件夹即可;
加上这句话后.classpath文件的内容如下:
然后导入到eclipse中即可,导入后 的目录结构为:
如果把项目部署到tomcat上的时候,会出现 Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules
这个问题,解决方法;
找到路径E:\workspace\Struts2Example\.settings下面的org.eclipse.wst.common.project.facet.core文件
修改 为 即可;
修改后的文件为:
然后再次进行部署就没有问题了。
首先创建:UserAction类:
-
package com.topwqp.action;
-
-
public class UserAction {
-
private String username;
-
-
public String getUsername() {
-
return username;
-
}
-
-
public void setUsername(String username) {
-
this.username = username;
-
}
-
-
public String execute(){
-
return "SUCCESS";
-
}
-
-
}
UserAction类很简单,不需要实现什么借口,或者继承什么类的, execute()方法是默认执行的方法,返回 SUCCESS是为了进行跳转转向。
login.jsp如下: 用到了struts2的标签库
-
<%@ page contentType="text/html; charset=UTF-8"%>
-
<%@ taglib prefix="s" uri="/struts-tags"%>
-
-
-
-
Struts 2 Hello World Example
-
-
"Welcome">
-
"username" label="Username" />
-
"password" label="Password" />
-
-
-
-
-
welcome.jsp内容如下:
-
<%@ page contentType="text/html; charset=UTF-8"%>
-
<%@ taglib prefix="s" uri="/struts-tags"%>
-
-
-
-
Struts 2 Hello World Example
-
-
-
Hello
-
"username" />
-
-
-
-
tomcat容器去加载web.xml文件,web.xml文件内容如下:
-
-
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
-
"" >
-
-
<web-app>
-
<display-name>Struts 2 Web Applicationdisplay-name>
-
-
<filter>
-
<filter-name>struts2filter-name>
-
<filter-class>
-
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
-
filter-class>
-
filter>
-
-
<filter-mapping>
-
<filter-name>struts2filter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
-
web-app>
web.xml回去加载struts.xml文件,其中配置了拦截的通配符路径。
struts.xml文件的内容如下:
其中package的name属性不起其他作用,仅仅是一个标记,namespace属性有重要作用,对应于webapp/user 这个目录的命名空间,所以当访问
时 就是访问对应的
两个访问效果是一样的。
-
xml version="1.0" encoding="UTF-8" ?>
-
-
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
-
"">
-
-
<struts>
-
-
<package name="user" namespace="/user" extends="struts-default">
-
<action name="Login">
-
<result>pages/login.jspresult>
-
action>
-
<action name="Welcome" class="com.topwqp.action.UserAction">
-
<result name="SUCCESS">pages/welcome.jspresult>
-
action>
-
package>
-
-
struts>
PS 如果是struts1 实现这个直接跳转的功能 代码如下:
-
<action
-
path="/Login"
-
type="org.apache.struts.actions.ForwardAction"
-
parameter="/pages/login.jsp"/>
跳转后如果提交就会提交到Welcome对应的action
路径为:
struts2的命名空间(namespace)对于模块化开发有很好的作用。
访问如下:
点击提交后页面挑战如下:
例子的下载地址:
Struts2Example.zip
阅读(4632) | 评论(0) | 转发(0) |