详细描述用maven构建Struts2项目

  • Post author:
  • Post category:其他



例子下载地址

1:

get started

2:

user guide

3:

API

4:

HOME

构建后目录结果如下:


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文件如下:




<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.topwqp</groupId>
  <artifactId>Struts2Example</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Struts2Example Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
	<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>
		
		<dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
 
  </dependencies>
  <build>
    <finalName>Struts2Example</finalName>
	<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
  </build>
</project>


配置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: http://uk.maven.org/maven2/org/apache/struts/struts2-core/2.3.1.2/s
truts2-core-2.3.1.2.pom
Downloaded: http://uk.maven.org/maven2/org/apache/struts/struts2-core/2.3.1.2/st
ruts2-core-2.3.1.2.pom (12 KB at 4.2 KB/sec)
Downloading: http://uk.maven.org/maven2/org/apache/struts/struts2-parent/2.3.1.2
/struts2-parent-2.3.1.2.pom
Downloaded: http://uk.maven.org/maven2/org/apache/struts/struts2-parent/2.3.1.2/
struts2-parent-2.3.1.2.pom (27 KB at 33.2 KB/sec)
Downloading: http://uk.maven.org/maven2/org/apache/struts/struts-master/8/struts
-master-8.pom
Downloaded: http://uk.maven.org/maven2/org/apache/struts/struts-master/8/struts-
master-8.pom (9 KB at 20.9 KB/sec)
Downloading: http://uk.maven.org/maven2/org/apache/apache/8/apache-8.pom
Downloaded: http://uk.maven.org/maven2/org/apache/apache/8/apache-8.pom (14 KB a
t 32.6 KB/sec)
Downloading: http://uk.maven.org/maven2/org/apache/struts/xwork/xwork-core/2.3.1
.2/xwork-core-2.3.1.2.pom
Downloaded: http://uk.maven.org/maven2/org/apache/struts/xwork/xwork-core/2.3.1.
2/xwork-core-2.3.1.2.pom (11 KB at 23.3 KB/sec)
Downloading: http://uk.maven.org/maven2/ognl/ognl/3.0.4/ognl-3.0.4.pom
Downloaded: http://uk.maven.org/maven2/ognl/ognl/3.0.4/ognl-3.0.4.pom (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: http://uk.maven.org/maven2/asm/asm/3.3/asm-3.3.pom
Downloaded: http://uk.maven.org/maven2/asm/asm/3.3/asm-3.3.pom (264 B at 0.6 KB/
sec)
Downloading: http://uk.maven.org/maven2/asm/asm-parent/3.3/asm-parent-3.3.pom
Downloaded: http://uk.maven.org/maven2/asm/asm-parent/3.3/asm-parent-3.3.pom (5
KB at 9.9 KB/sec)
Downloading: http://uk.maven.org/maven2/asm/asm-commons/3.3/asm-commons-3.3.pom
Downloaded: http://uk.maven.org/maven2/asm/asm-commons/3.3/asm-commons-3.3.pom (
415 B at 1.0 KB/sec)
Downloading: http://uk.maven.org/maven2/asm/asm-tree/3.3/asm-tree-3.3.pom
Downloaded: http://uk.maven.org/maven2/asm/asm-tree/3.3/asm-tree-3.3.pom (404 B
at 1.0 KB/sec)
Downloading: http://uk.maven.org/maven2/org/freemarker/freemarker/2.3.18/freemar
ker-2.3.18.pom
Downloaded: http://uk.maven.org/maven2/org/freemarker/freemarker/2.3.18/freemark
er-2.3.18.pom (2 KB at 2.9 KB/sec)
Downloading: http://uk.maven.org/maven2/org/apache/struts/struts2-core/2.3.1.2/s
truts2-core-2.3.1.2.jar
Downloaded: http://uk.maven.org/maven2/org/apache/struts/struts2-core/2.3.1.2/st
ruts2-core-2.3.1.2.jar (757 KB at 100.6 KB/sec)
Downloading: http://uk.maven.org/maven2/org/apache/struts/xwork/xwork-core/2.3.1
.2/xwork-core-2.3.1.2.jar
Downloaded: http://uk.maven.org/maven2/org/apache/struts/xwork/xwork-core/2.3.1.
2/xwork-core-2.3.1.2.jar (606 KB at 61.6 KB/sec)
Downloading: http://uk.maven.org/maven2/ognl/ognl/3.0.4/ognl-3.0.4.jar
Downloaded: http://uk.maven.org/maven2/ognl/ognl/3.0.4/ognl-3.0.4.jar (223 KB at
 89.6 KB/sec)
Downloading: http://uk.maven.org/maven2/asm/asm/3.3/asm-3.3.jar
Downloaded: http://uk.maven.org/maven2/asm/asm/3.3/asm-3.3.jar (43 KB at 22.9 KB
/sec)
Downloading: http://uk.maven.org/maven2/asm/asm-commons/3.3/asm-commons-3.3.jar
Downloaded: http://uk.maven.org/maven2/asm/asm-commons/3.3/asm-commons-3.3.jar (
38 KB at 31.4 KB/sec)
Downloading: http://uk.maven.org/maven2/asm/asm-tree/3.3/asm-tree-3.3.jar
Downloaded: http://uk.maven.org/maven2/asm/asm-tree/3.3/asm-tree-3.3.jar (21 KB
at 35.6 KB/sec)
Downloading: http://uk.maven.org/maven2/org/freemarker/freemarker/2.3.18/freemar
ker-2.3.18.jar
Downloaded: http://uk.maven.org/maven2/org/freemarker/freemarker/2.3.18/freemark
er-2.3.18.jar (903 KB at 118.5 KB/sec)
Downloading: http://uk.maven.org/maven2/commons-io/commons-io/2.0.1/commons-io-2
.0.1.jar
Downloaded: http://uk.maven.org/maven2/commons-io/commons-io/2.0.1/commons-io-2.
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文件在第一行加上

<classpathentry kind="src" path="src/main/java" including="**/*.java"/>

然后在相应的目录下创建java的文件夹即可;

加上这句话后.classpath文件的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.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="var" path="M2_REPO/org/apache/struts/xwork/xwork-core/2.3.1.2/xwork-core-2.3.1.2.jar"/>
  <classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/2.5/commons-lang-2.5.jar"/>
  <classpathentry kind="var" path="M2_REPO/ognl/ognl/3.0.4/ognl-3.0.4.jar"/>
  <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/org/freemarker/freemarker/2.3.18/freemarker-2.3.18.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"/>
</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文件

修改 <installed facet=”jst.web” version=”3.0″/> 为  <installed facet=”jst.web” version=”2.4″/> 即可;

修改后的文件为:

<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
  <fixed facet="jst.java"/>
  <fixed facet="jst.web"/>
  <installed facet="jst.web" version="2.4"/>
  <installed facet="jst.java" version="1.7"/>
</faceted-project>

然后再次进行部署就没有问题了。

首先创建: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"%>
<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.jsp内容如下:

<%@ 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>

tomcat容器去加载web.xml文件,web.xml文件内容如下:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<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>

web.xml回去加载struts.xml文件,其中配置了拦截的通配符路径。

struts.xml文件的内容如下:

其中package的name属性不起其他作用,仅仅是一个标记,namespace属性有重要作用,对应于webapp/user 这个目录的命名空间,所以当访问

http://localhost:8080/Struts2Example/user/Login.action时  就是访问对应的http://localhost:8080/Struts2Example/user/pages/login.jsp

两个访问效果是一样的。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
    <package name="user" namespace="/user" extends="struts-default">
        <action name="Login">
            <result>pages/login.jsp</result>
        </action>
        <action name="Welcome" class="com.topwqp.action.UserAction">
            <result name="SUCCESS">pages/welcome.jsp</result>
        </action>
    </package>
 
</struts>

PS 如果是struts1 实现这个直接跳转的功能 代码如下:

	<action
			path="/Login"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/pages/login.jsp"/>

跳转后如果提交就会提交到Welcome对应的action

路径为:http://localhost:8080/Struts2Example/user/Welcome.action

struts2的命名空间(namespace)对于模块化开发有很好的作用。

访问如下:


点击提交后页面挑战如下:




















版权声明:本文为topwqp原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。