前面我們已經提到了在Eclipse中怎么配置struts2和Eclipse中怎么配置maven。如今要將這兩者結合起來,利用maven的優(yōu)點,使得struts2項目更易管理。
關于struts2環(huán)境搭建和maven配置參見擴展閱讀。
利用maven創(chuàng)建web項目
首先在File中選擇Maven Project
Maven Project
接著選擇工作空間路徑,這里我選擇默認路徑
Paste_Image.png
選擇創(chuàng)建webapp項目
Paste_Image.png
根據項目實際輸入Group id和Artifact id
Paste_Image.png
最后finish,這樣就成功創(chuàng)建了一個maven的web項目。項目創(chuàng)建成功的目錄樹如下
Paste_Image.png
但是,這時我們注意到Java Resources目錄下沒有出現 src/main/java 和 src/test/java兩個目錄,這兩個目錄是存放java源代碼的目錄。解決辦法如下:
- 右鍵項目->Build Path->Configure Build Path
Paste_Image.png
- 進入Order and Export目錄,勾選兩個缺失的庫
Paste_Image.png
- 保存修改并應用
可以看到Java Resources目錄下出現了src/main/java和src/test/java目錄。這樣才算最終成功創(chuàng)建了maven的web項目。
maven的struts2項目實例
- 導入struts2庫
在maven中導入庫不再需要你手動復制庫文件,只需要你修改pom.xml,maven會自動下載相應的庫到本地。
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.demo</groupId>
<artifactId>MavenDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MavenDemo 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>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies>
<build>
<finalName>MavenDemo</finalName>
</build>
</project>
一共導入了三個依賴包。分別是junit、javax.servlet、struts2-core這三個包。
修改后,別忘了更新Maven項目。右鍵項目->Maven->Update Project,然后就可以看到maven已經將依賴包下載到本地了。
Paste_Image.png
- 添加動作類(Action)
前面提到src/main/java是用來存放java代碼的,這里我們在里面放一個簡單的動作類。
Paste_Image.png
登陸動作,只有UserName的getter和setter
package com.demo;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String Name;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
- 添加視圖(View)
視圖文件存在webapp目錄下,這里我們也只需創(chuàng)建兩個視圖。
Paste_Image.png
首頁視圖,用戶輸入姓名,并提交
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h2>Hello World!</h2>
<form action="loginAction">
姓名<input type='text' name="Name"><input type="submit" value="提交">
</form>
</body>
</html>
結果視圖,返回用戶姓名
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page 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>Welcome</title>
</head>
<body>
Hello <s:property value="Name" />
</body>
</html>
- 配置struts.xml
這里在src/main/resources中創(chuàng)建一個struts.xml文件,并在其中進行修改。
Paste_Image.png
創(chuàng)建一個動作類到視圖的一個映射
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.locale" value="zh_CN"></constant>
<package name="hurricane" extends="struts-default">
<action name="loginAction" class="com.demo.LoginAction" method="execute">
<result>
/result.jsp
</result>
</action>
</package>
</struts>
- 最后修改web.xml
web.xml是一個任何對struts2請求的入口點。Struts2應用程序的入口點是在web.xml中定義的過濾器。這些都需要在web.xml中進行聲明
Paste_Image.png
web.xml的代碼如下:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
注:不同版本的struts2這里對應的過濾器不同,可能需要針對專門查看專門的struts2說明。
- 運行項目實例
右鍵項目->Run->Run as Server就可以看到項目運行情況,運行情況如下:
index.jsp
result.jsp
至此完成了Maven中struts2實例的配置。