使用Maven搭建Struts2專案
阿新 • • 發佈:2019-02-08
大家好 今天我們來使用maven構建Struts2專案,關於maven的環境搭建請參考 Maven環境搭建和介紹。
1. Struts2框架的環境配置
1.1下載
進入apache的官方網:https://dist.apache.org/repos/dist/release/struts/這裡為大家提供了struts的各種版本,供大家使用。
下載完後,解壓到本地磁碟,該資料夾包含如下檔案結構:
apps:該資料夾下包含了struts 2 的示例應用。
docs:struts2的相關文件,包含struts2的快速入門、struts2的幫助文件及API文件等內容。
lib:該資料夾下包含了struts2框架的核心類庫,以及struts2的第三方外掛類庫。
src:該檔案下包含了struts2框架的全部原始碼。
1.2新增Struts2依賴
這裡主需要在pom.xml中新增一個struts-core的依賴即可(這裡所使用的是 struts 2.3.14版本):<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>struts2_project</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>struts2 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>4.9</version> <scope>test</scope> </dependency> <!-- struts2依賴包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.14</version> </dependency> </dependencies> <build> <finalName>struts2</finalName> </build> </project>
1.3新建一個Action
在src/main/Java目錄下新建一個UserAction.java
package com.demo.action; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; public String login() { try { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("name->" + username + ",password->" + password); if ("admin".equals(username) && "123456".equals(password)) { return SUCCESS; } else { return "login"; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return SUCCESS; } }
1.4配置Struts.xml
在下載的struts2包裡面,解後會有一個apps資料夾,開啟之後裡面有一些官方給你的參考示例。隨便開啟一個,裡面就會有struts.xml檔案,直接把這個檔案複製過來再修改就可以了,如下:<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.demo.action.UserAction" method="login">
<result name="success">index.jsp</result>
<result name="login">login.jsp</result>
</action>
</package>
</struts>
1.5配置web.xml
1編輯web應用的web.xml配置檔案,配置struts2的核心Filter,這個可以從Struts2官網(http://struts.apache.org/development/2.x/docs/webxml.html)找到示例。2前面我們提到Maven構建web專案時資原始檔存放在src/main/resources目錄下,但是Struts2的配置檔案struts.xml預設要放在類路徑下,也就是src下。
這個位置可以修改,就是在web.xml中加入以下內容:
<init-param>
<param-name>config</param-name>
<param-value>../../resources/struts.xml</param-value>
</init-param>
配置完成後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>Archetype Created Web Application</display-name>
<init-param>
<param-name>config</param-name>
<param-value>../../resources/struts.xml</param-value>
</init-param>
<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>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.測試
新建兩個頁面login.jsp,index.jsp,內容如下: 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">
<table>
<tr>
<td>使用者名稱:</td>
<td><input type="text" name="username" /> </td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="text" name="password" /> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登入" />
<input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
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>Hello Maven</title>
</head>
<body>
<p>大家好,歡迎進入Maven Struts2應用!</p>
</body>
</html>
注意點:在struts 2.5.2版本之後 ,struts的結構發生了改變。具體請參考 Struts2 2.5.2的套路