用Eclipse配置一個簡單的struts2,網上的水太深,坑多,各種折磨。下面就是總結並給出明確的步驟,保證一次到位。當然,版本有差異。
在Eclipse中選擇File-New-Dynamic Web Project創建動態項目Struts2Demo
下面先給出項目的代碼以及工程路徑截圖:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
<!--注意:一些較低的版本,類名中是沒有ng這個名稱的,建議下載稍高一些的版本-->
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern><!--這裏建議用*.action,使用/*容易出錯 -->
</filter-mapping>
2、配置struts2.xml ,注意該文件放的位置,放在工程目錄的src下面(如上圖所示位置),服務器運行時會自動加載到META-INF目錄下面的classes文件夾,默認是不顯示這個文件的。
<?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">
<!--這段語句得加,否則顯示警告標示,2.3對應你下載的struts版本號 -->
<struts>
<package name="com.lx1991.action" extends="struts-default">
<!--這個包名要與在目錄src下面建立的com.lx1991.action包名一致 -->
<action name="LoginAction" class="com.lx1991.action.LoginAction">
<!--注意:這個name名稱與login.jsp裏form表單的action="LoginAction.action"一致-->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
3、在工程目錄WebContent根目錄下面創建login.jsp,用來提交用戶名(username)和密碼(password)
<body>
<!--LoginAction就是struts.xml裏面com.lx1991.action.LoginAction的URL對應的名稱(name) -->
<form action="LoginAction.action" method="post">
用戶名:<input type="text" name="username" /><br>
密碼:<input type="password" name="password" /><br>
<input type="submit" value="提交" />
</form>
</body>
4、在工程目錄WebContent根目錄下面創建success.jsp,login.jsp提交的內容經過LoginAction.java類執行,返回的結果經過struts2.xml進行處理,然後轉到相應的頁面。(例如,輸入的用戶名和密碼正確,轉到success.jsp,否則轉到error.jsp頁面)
<body>
<font color="red" size="20">Success's HelloWorld.. </font>
</body>
5、在工程目錄WebContent根目錄下面創建error.jsp
<body>
<font color="red" size="20">Error's Page</font>
</body>
6、在工程目錄-->Java resources-->src文件下面,新建包(本例包名爲:com.lx1991.action), 並在包下面創建LoginAction.java類
package com.lx1991.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
//這裏繼承ActionSupport
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
if("lx".equals(username)&&"123".equals(password)){//簡單驗證用戶名和麪膜
return "success";
}else{
return "error";
}
}
}
7、點擊工程Struts2Demo名,右鍵-->Run As-->Run on Server-->Apache Tomcat 7.0執行。
訪問路徑:http://localhost:8080/Struts2Demo/login.jsp
注意事項:如果,login.jsp頁面使用struts2標籤的如下面的代碼,那麼訪問路徑爲
http://localhost:8080/Struts2Demo/login.action(暫時不建議login.jsp使用struts標籤)
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login</title>
</head>
<body>
<!--helloWorld就是struts.xml裏面HelloWorld的URL -->
<s:form action="HelloWorld.action" method="post">
<s:textarea name="username" label="用戶名" /><br>
<s:password name="password" label="密碼" /><br>
<s:submit value="提交" />
<s:reset value="重置" />
</s:form>
</body>
</html>