web.xml
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.tw511.common</param-value> </init-param> </filter>
從測試(Struts22.1.6和2.1.8版本),這是不正確的,不管你把在「param-value」還是 「struts.convention.action.packages「, 在Struts 2會忽略它,並只掃描指定的檔案夾命名:struts, struts2, action 或 actions 。
詳細請檢視這裡Struts 2 約定外掛檔案
例如 : LoginAction.java
最終的專案結構
擴充套件ActionSupport並建立了LoginAction,什麼也不做,ActionSupport 預設返回 「success」 字串,這將匹配 @Result 並重定位到 「pages/login.jsp「.
註解版本
package com.yiibai.user.action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.ResultPath; import com.opensymphony.xwork2.ActionSupport; @Namespace("/User") @ResultPath(value="/") @Result(name="success",location="/login.jsp") public class LoginAction extends ActionSupport{ }
XML 實現版本
<package name="user" namespace="/User" extends="struts-default"> <action name="Login"> <result>/login.jsp</result> </action> </package>
重寫execute()方法並指定 @Action 和 @Result 註解。
註解版本
package com.yiibai.user.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.ResultPath; import com.opensymphony.xwork2.ActionSupport; @Namespace("/User") @ResultPath(value="/") public class WelcomeUserAction extends ActionSupport{ private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Action(value="Welcome", results={ @Result(name="success",location="welcome_user.jsp") }) public String execute() { return SUCCESS; } }
XML 實現版本
<package name="user" namespace="/User" extends="struts-default"> <action name="Welcome" class="com.yiibai.user.action.WelcomeUserAction"> <result name="SUCCESS">/welcome_user.jsp</result> </action> </package>
login.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head></head> <body> <h1>Struts 2 註解範例</h1> <s:form action="Welcome"> <s:textfield name="username" label="使用者名" /> <s:password name="password" label="密碼" /> <s:submit value="提交"/> </s:form> </body> </html>
welcome_user.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head></head> <body> <h1>Struts 2 註解範例</h1> <h4>您好, <s:property value="username"/></h4> </body> </html>
只要建立一個典型的web.xml檔案,並宣告FilterDispatcher過濾器標準。
<!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>Struts 2 Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
http://localhost:8080/struts2example/User/login.action
提交到 http://localhost:8080/Struts2Example/User/Welcome.action 後顯示: