在前兩篇博客中,使用Maven構建了Web項目,在這篇博客中寫一下,怎樣構建一個簡單的Struts2項目。
在準備過程中發現,要使用好Maven,個人覺得要好好利用這兩個網站:
[http://mvnrepository.com/](http://mvnrepository.com/)
[http://search.maven.org/](http://search.maven.org/)
由于自己對Maven的理解不是非常深,所以學習的時候遇到很多簡單的問題都沒法解決
,走了很多彎路
### 1. 新建一個基本的Web項目
這和前面講的是一樣的,可以參考前面的博客
### 2. 添加Struts2依賴
這里主需要在pom.xml中添加一個struts-core的依賴即可:
~~~
<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.deppon.demo</groupId>
<artifactId>test02</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test02 Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 屬性配置 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 添加JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Struts2 依賴 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<finalName>test02</finalName>
</build>
</project>
~~~
之后,Maven會自動從網上下載struts2需要的其他依賴包,可以看一下這里:

是不是都有了,有的時候Maven可能會報錯,由于網絡的原因(個人認為大多是網絡問題,導致下載依賴包出錯
),可以從上面提到的兩個網站手動下載
### 3. 新建一個Action
~~~
package com.deppon.test02.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = -1417237614181805435L;
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* 跳轉到登錄界面
* @return
*/
public String login_input() {
return SUCCESS;
}
/**
* 登錄
* @return
*/
public String login() {
System.out.println("name->" + name);
System.out.println("password->" + password);
return SUCCESS;
}
}
~~~
struts.xml
~~~
<?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>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.multipart.maxSize" value="20971520"/>
<constant name="struts.devMode" value="true" />
<package name="p_user" namespace="/" extends="struts-default">
<action name="login_input" class="com.deppon.test02.action.UserAction" method="login_input">
<result name="success">
/login.jsp
</result>
</action>
<action name="login" class="com.deppon.test02.action.UserAction" method="login">
<result name="success">
/login_success.jsp
</result>
</action>
</package>
</struts>
~~~
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>
<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>
~~~
index.jsp
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主頁</title>
</head>
<body>
<a href="login_input">去登陸</a>
</body>
</html>
~~~
login.jsp
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登錄界面</title>
</head>
<body>
<form action="login" method="post">
name:<input type="text" name="name" />
password:<input type="password" name="password" />
<input type="submit" value="登錄" />
</form>
</body>
</html>
~~~
login_success.jsp
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登錄成功</title>
</head>
<body>
<s:form action="login" namespace="/" method="post">
<s:textfield name="name" label="name"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
~~~
項目結構如下圖所示:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- [Maven學習(一)- 環境搭建](http://blog.csdn.net/jolingogo/article/details/8775046)
- [Maven學習(二)- 安裝m2eclipse插件?](http://blog.csdn.net/jolingogo/article/details/8796410)
- [Maven學習(三)- 使用Maven構建Web項目](http://blog.csdn.net/jolingogo/article/details/8796726)
- [Maven學習(四)- 使用Maven構建Web項目-測試](http://blog.csdn.net/jolingogo/article/details/8797153)
- [Maven學習(五)- 使用Maven構建Struts2項目](http://blog.csdn.net/jolingogo/article/details/8798052)
- [Maven學習(六)- 構建Hibernate項目](http://blog.csdn.net/jolingogo/article/details/8798684)
- [Maven學習(七)- 構建Spring項目](http://blog.csdn.net/jolingogo/article/details/8799307)
- [Maven學習(八)- 構建MyBatis項目](http://blog.csdn.net/jolingogo/article/details/8801158)
- [Maven學習(九)- 構建SSH項目](http://blog.csdn.net/jolingogo/article/details/8811817)
- [Maven學習(十) - 階段小結
](http://blog.csdn.net/jolingogo/article/details/8821375)
- [專欄:Maven學習之旅](http://blog.csdn.net/column/details/yuguiyang-maven.html)
- 前言
- (一)- 環境搭建
- (二)- 安裝m2eclipse插件
- (三)- 使用Maven構建Web項目
- (四)- 使用Maven構建Web項目-測試
- (五)- 使用Maven構建Struts2項目
- (六)- 構建Hibernate項目
- (七)- 構建Spring項目
- (八)- 構建MyBatis項目
- (九)- 構建SSH項目
- (十) - 階段小結
- Maven深入學習(一)- 坐標
- Maven深入學習(二)- 依賴
- Maven深入學習(三)- 聚合與繼承
- Maven深入學習(四)- 知識總結
- Maven創建的Web項目無法使用EL表達式
- Maven知識點記錄 - profile
- Maven知識點記錄 - repositories
- Maven最佳實踐:版本管理
- Ubuntu上安裝Maven3
- Maven常用命令-創建Java項目
- Maven常用命令-創建Web項目
- Maven中引入本地jar包
- Maven私服(一) - The nexus service was launched, but failed to start.
- Maven私服(二) - Nexus的安裝