Struts框架汲取了Struts的優點,以WebWork為核心,攔截器,可變和可重用的標簽。
第一步:加載Struts2 類庫:
第二步:配置web.xml
<?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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置Struts2過濾器 :攔截請求 --> <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> </web-app>
第三步:開發視圖層頁面(提交表單頁)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Struts2Demo</title> </head> <body> <h1>HelloWorld Struts2</h1> <hr> <form action="LonginAction" method="post"> <p>用戶名:<input type="text" name="username" /></p> <p>密 碼:<input type="password" name="password" /></p> <p><input type="submit" /></p> </form> </body> </html>
四:三層架構:
package dao; /** * 用戶操作接口 * @author Administrator * */ public interface UserDao { public String login(String username,String password); }
package biz; /** * 用戶業務接口 * @author Administrator * */ public interface UserBiz { public String login(String username,String password); }
package biz.impl; import biz.UserBiz; import dao.UserDao; import dao.UserDaoImpl; public class UserBizImpl implements UserBiz { //創建dao層對象 UserDao userdao = new UserDaoImpl(); /** * 調用dao層方法 */ public String login(String username, String password) { return userdao.login(username, password); } }
package dao; public class UserDaoImpl implements UserDao { public String login(String username, String password) { String str = ""; if(username.equals("msit") && password.equals("123456")){ str = "success"; }else{ str = "error"; } // TODO Auto-generated method stub return str; } }
第五步:開發控制層Action
package Action; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import biz.UserBiz; import biz.impl.UserBizImpl; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 登錄控制器 * @author Administrator * */ public class LoginAction extends ActionSupport{ /*值棧:頁面可以直接獲取*/ String username;//form表單name;進行封裝 String password;//form表單name;進行封裝 //創建Biz層對象 UserBiz userbiz = new UserBizImpl(); /* (non-Javadoc) * @see com.opensymphony.xwork2.ActionSupport#execute() */ @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println(username+"==="+password); /*解耦(降低依賴)方式;基於Map集合*/ ActionContext ac = ActionContext.getContext(); /*得到request*/ Map request = (Map) ac.get("request"); //request.put("username", username); /*得到session*/ Map session = ac.getSession(); /*得到application*/ Map application = ac.getApplication(); //=================================================== /*耦合(依懶性)方式*/ HttpServletRequest request2 = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); HttpSession session2 = request2.getSession(); //request2.setAttribute("username", username); /** * struts默認提供了五個狀態字符串 */ return userbiz.login(username, password); } /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } }
第六步:配置struts.xml(核心文件);可以參照struts-default.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" namespace="/" extends="struts-default"> <!-- 配置Action:關聯Action JavaBean --> <action name="LonginAction" class="Action.LoginAction"> <!-- 指定返回的視圖 ;默認使用轉發--> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts>
第七步:處理完邏輯之后返回相應的字符串success、error跳轉到相應的頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> ${username } 登陸成功!!! <br> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'error.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 登錄失敗 <br> </body> </html>
注意:
配置struts2 1、加入類庫(jar包)—從應用實例當中拷貝 2、在web.xml中配置過濾器 <!-- 配置Struts2過濾器 --> <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> 3、編寫頁面index.jsp 4、配置控制器(action) LoginAction 繼承 Actionsupper 重寫父類的excute()方法 5、配置struts.xml(核心文件);可以參照struts-default.xml 6、處理完邏輯之后返回相應的字符串success、error跳轉到相應的頁面