一.Struts2基礎html
1.Struts2是有Apache在Struts1和Webwork的基礎上研發出的新一代MVC框架。java
2.Struts2開發環境的搭建:web
打開https://struts.apache.org/download.html頁面下載Struts2的版本,我使用是:struts-2.3.14-all.zipapache
二.HelloWorld示例瀏覽器
導入到項目中,而後加載到項目中(自行百度不細說)app
2.新建一個FirstAction.java框架
package com.scce.struts2.action; import java.text.SimpleDateFormat; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class FirstAction extends ActionSupport { private String date; private String message; @Override public String execute() throws Exception { SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH時ss分mm秒"); date=sdf.format(new Date()); message="Hello World"; return SUCCESS; } public String getDate(){ return date; } public String getMessage(){ return message; } }
3.新建一個struts.xml文件jsp
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="action" namespace="/" extends="struts-default"> <action name="FirstAction" class="com.scce.struts2.action.FirstAction"> <result>/firstStruts2.jsp</result> </action> </package> </struts>
4.修改web.xmlide
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <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>index.jsp</welcome-file> </welcome-file-list> </web-app>
5.新建一個firstStruts2.jsp頁面post
<body> ${message } <s:property value="date"/><br> ${date} </body>
6.瀏覽器訪問地址:http://localhost:8080/struts2_01/FirstAction.action
效果以下圖:
三.屬性驅動模型
(註解:Struts2與ServletAPI實現瞭解耦,因此沒法直接使用HttpServletRequest對象獲取表單提交的參數,但Strtus2提供了屬性驅動模型機制來解決問題)
<form action="UserManagerAction.action" method="post"> <p>用戶名:<input type="text" name="username"/>${messsage}</p> <p>密 碼:<input type="text" name="password"/></p> <input type="submit" value="提交"/> </form>
package com.scce.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class UserManagerAction extends ActionSupport { public String getMesssage() { return messsage; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } private String username;//用戶名 private String password;//密碼 private String messsage;//消息 @Override public String execute() { System.out.println("用戶名"+username);//打印用戶名 System.out.println("密碼"+password);//密碼 //默認帳號admin,密碼1234驗證登錄成功 if("admin".equals(username)&&"1234".equals(password)) { messsage="登錄成功!ȷ"; return "success"; } else { messsage="對不起帳號或密碼錯誤"; return "error"; } } }
3.在struts.xml文件中配置UserManagerAction類的相關信息
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="action" namespace="/" extends="struts-default"> <action name="FirstAction" class="com.scce.struts2.action.FirstAction"> <result>/firstStruts2.jsp</result> </action> <action name="UserManagerAction" class="com.scce.struts2.action.UserManagerAction"> <result name="success">/login.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
4.在瀏覽器訪問地址:
http://localhost:8080/struts2_01/UserManagerAction.action