一。Struts2.0簡介
Struts2.0是從WebWork基礎上發展起來的.從某種程度上來講,Struts2沒有繼承Struts1的血統,而是繼承了WebWork的血統.或者說,WebWork衍生出了Struts2,而不是Struts1衍生了Struts2.因為Struts2是WebWork的升級,而不是一個全新的框架,因此穩定性等各方面都有很好的保證:而且吸收了Struts1和WebWork兩者的優勢。
二.為什么要使用struts2.0
三.Struts2.0的MVC架構
控制器是控制请求的转发和拦截。控制器将请求转发到相应的Action然后填充视图,从而进行展现。
四. Struts2.0的核心組件
五. Struts2.0的基本流程
六. Struts2的基本流程
FilterDispatcher是struts2框架的核心控制器,該控制器作為一個Filter運行在Web應用中,它負責攔截所有的用戶請求,當用戶請求到達時,該Filter會鍋爐用戶請求.如果用戶請求以action結尾 ,該請求將被轉入Struts2框架處理. Struts2框架獲得了.action請求后,將根據*.action請求的前面部分決定調用哪個業務邏輯組件,例如,對于login.action請求,Struts2調用名為login的Action來處理該請求. struts2應用中的Action都被定義在struts.xml文件中,在該文件中定義Action時,定義了該Action的name屬性和class屬性,其中name屬性決定了該Action處理哪個用戶請求,而class屬性決定了該Action的實現類. Struts2用于處理用戶請求的Action實例,并不是用戶實現的業務控制器,而是Action代理--因為用戶實現的業務控制器并沒有和Servlet API耦合,顯然無法處理用戶請求.而Struts2框架提供了系列攔截器,該系列攔截器負責將HttpServletRequest請求中的請求參數解析出來,傳入到Action中,并回調Action的execute方法來處理用戶請求.
七. 代碼分析
1.
JSP頁面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head>
<body> <form action="login.action"> 姓名:<input/> <br> 密碼:<input/> <br> <input value="注冊"/> </form> </body>
</html>
struts.xml配置文件
<?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> <!--Struts 2的Action都必須配置在package里--> <packagemso-fareast-font-family: 新細明體; mso-hansi-font-family: Arial; mso-ansi-language: EN-US">ee"" extends="struts-default"> <!--定義一個Login的Action,實現類為com.mateng.LoginAction --> <action> <!--配置Action返回success時轉入/ welcome.jsp頁面--> <result>/welcome.jsp</result> <result>/error.jsp</result> </action> </package> </struts>
Java文件
public class LoginAction { private String name; private String password; ………這里省略get,set方法 public String execute() { if(name.equals(“Eric")&&password.equals(“123”)){ ActionContext.getContext().getSession().put(“name”); return "success"; }else{ return "error"; } } }
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!--web > <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>*.action</url-pattern> </filter-mapping> </web-app>
2.
多個action寫在一個類中
struts.xml配置文件(多個action寫在一個類中)
<?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> <!--Struts 2的Action都必須配置在package里--> <packageFONT-SIZE: 16pt; mso-fareast-font-family: 新細明體; mso-hansi-font-family: Arial; mso-ansi-language: EN-US">ee"" extends="struts-default"> <actionFONT-SIZE: 16pt; mso-fareast-font-family: 新細明體; mso-hansi-font-family: Arial; mso-fareast-language: ZH-CN">stuRegister"stuRegister"> <result>/welcome.jsp</result> <result>/error.jsp</result> </action> <action com.mateng.StudentAction“ method="studentLogin"> <result> <param>findAllPlain.action</param> </result> <result>/stulogin.jsp</result> </action></package> </struts>
Java文件(多個action寫在一個類中)
public class StudentAction extends ActionSupport{ private String name; private String password; ………這里省略get,set方法 public String stuRegister() throws Exception{ …………. } public String studentLogin() throws Exception{ …………. } }






#1 by tengteng on 三月 17th, 2010 - 10:44 上午
讲的太好了!