1. Struts2的安裝配置
1.1下載
進入apache的官方網:http://struts.apache.org/download.cgi#struts2316下載struts2的GA完整版,Struts 2.3.16.3是Struts 2.3.X系列最穩定的版本。下載完後,解壓到本地磁盤,該文件夾包含如下文件結構:
apps:該文件夾下包含了struts 2 的示例應用。
docs:struts2的相關文檔,包含struts2的快速入門、struts2的幫助文檔及API文檔等內容。
lib:該文件夾下包含了struts2框架的核心類庫,以及struts2的第三方插件類庫。
src:該文件下包含了struts2框架的全部源代碼。
1.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.carson.demo</groupId>
<artifactId>struts2</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>
之後,Maven會自動從網上下載struts2需要的其他依賴包,可以看一下這裏:1.3新建一個Action
在src/main/java目錄下新建一個UserAction.java
package com.carson.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 execute(){
return SUCCESS;
}
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>
<constant name="struts.i18n.reload" value="false" />
<constant name="struts.devMode" value="false" />
<include file="struts-default.xml" />
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.carson.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>
項目結構如下圖所示:
啓動之後就可以看到登錄頁面,至此,用Maven構建Struts2項目完成。