Today is first day of J2E高級部分。今天開始講解struts1,為什么要講1不講2呢?因?yàn)樽?/span>struts1以來,就應(yīng)用廣泛。即使Struts2已經(jīng)發(fā)布了,但使用struts1開發(fā)的WEB應(yīng)用比較多見,因?yàn)?/span>Struts2與Struts1有很大的差別,所以將以前使用Struts1開發(fā)的WEB應(yīng)用移植到Struts2上來,不是一件容易的事。所以大家在工作中,可能會(huì)遇到有使用Struts1的WEB應(yīng)用。這是學(xué)習(xí)Struts1的原因,但我認(rèn)為學(xué)習(xí)Struts1可以讓我在框架這片地方混的熟悉一些,學(xué)學(xué)框架的設(shè)計(jì),而且過渡到Struts2也十分容易!
Struts1的課程由佟剛老師教授,佟老師也是傳智的一位名人。今日一見,名不虛傳!老佟十分幽默,而且細(xì)心。課上給同學(xué)們帶來歡笑的同時(shí),課程內(nèi)容也一一被它記錄在記事文本文件中并放入工程。邏輯思路清晰,就是講課有點(diǎn)快。他說以后會(huì)慢慢提速,直到大家適應(yīng)了快速。其實(shí)后續(xù)課程中還有項(xiàng)目等著大家呢,如果速度不提快,大家不努力適應(yīng)上來,怎能做好項(xiàng)目。這是一個(gè)適應(yīng)過程,保質(zhì)保量的完成項(xiàng)目!
既然很快,今天的內(nèi)容非常多。下面,我來總結(jié)一下主要內(nèi)容。
一、為什么要使用Struts
在之前的學(xué)習(xí)過程中,我們編寫的練習(xí)程序有以下特點(diǎn)。
1. 使用 MVC 設(shè)計(jì)模式
1). 原則: 所有的請求都必須提交到 Servlet。
2). Servlet的職責(zé):
①. 接受請求, 獲取請求參數(shù)。
②. 進(jìn)行簡單驗(yàn)證,比如用戶名、密碼、email格式是否正確...。
③. 封裝數(shù)據(jù)到一個(gè)JavaBean,比如將用戶信息封裝到UserBean中。
④. 調(diào)用方法,處理業(yè)務(wù)邏輯。
⑤. 確定要派發(fā)的頁面。
⑥. 派發(fā)頁面。
3). 使用Servlet作為控制器有以下不足:
①. 若Servlet僅負(fù)責(zé)頁面派發(fā),此時(shí)Servlet有些浪費(fèi)。
②. 在Servlet中進(jìn)行簡單驗(yàn)證,導(dǎo)致Servlet中的代碼比較臃腫。
③. 因?yàn)橐砂l(fā)的頁面寫在了Servlet的代碼中,若需要更改派發(fā)的頁面,則需要修改源代碼。
④. 進(jìn)行國際化,比較麻煩。
⑤. 處理表單重復(fù)提交,文件的上傳操作,表單的回顯等常用功能也比較麻煩。
4). Struts可以解決上述問題
二、Struts原理與應(yīng)用
1.Struts流程:
Servlet/JSP 容器 http請求 http響應(yīng) | |

圖中每個(gè)Action都可以設(shè)置一個(gè)FormBean用于校驗(yàn)頁面提交的form數(shù)據(jù)。
2.我們來編寫一個(gè)校驗(yàn)用戶注冊信息的簡單應(yīng)用
index.jsp,我直接把這個(gè)頁面設(shè)置為注冊頁面(可以將名字改為“register.jsp”)。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table align="center"> <form action="${pageContext.request.contextPath }/reg.do" method="post"> <tr> <td>用戶名:</td><td><input type="text" name="username" value="${param.username }"/></td> <td><html:errors property="username" /></td> </tr> <tr> <td>密碼:</td><td><input type="password" name="password"/></td> <td><html:errors property="password"/></td> </tr> <tr> <td>確認(rèn)密碼:</td><td><input type="password" name="password2"/></td> <td><html:errors property="password2"/></td> </tr> <tr> <td>生日:</td><td><input type="text" name="birthday" value="${param.birthday }"/></td> <td><html:errors property="birthday"/></td> </tr> <tr> <td><input type="submit" value="注冊"/></td> <td><input type="reset" value="重填"/></td> </tr> </form> </table> </body> </html> |
注意里邊的“<html:errors…”,它是獲取Struts框架中ActionForm生成的錯(cuò)誤信息。關(guān)于ActionForm請繼續(xù)向下看。
web.xml,WEB應(yīng)用的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>091220StrutsLogin</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
注意其中的“org.apache.struts.action.ActionServlet”類,它是Struts的控制器,所有“*.do”的請求都交由它處理。它需要一個(gè)初始化參數(shù)——struts-config.xml(Struts的配置文件)!在的開發(fā)中,如果不記得web.xml的配置方式,可以到struts包目錄下的apps目錄下解壓一個(gè)示例文件(*.war),在示例文件中有相關(guān)配置。
struts-config.xml,struts框架的配置文件:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="regForm" type="cn.itcast.cc.forms.RegForm"> </form-bean> </form-beans> <action-mappings> <action path="/reg" type="cn.itcast.cc.actions.Register" validate="true" name="regForm" input="/index.jsp"> <forward name="success" path="/success.jsp"></forward> </action> </action-mappings> <message-resources parameter="MessageResources"></message-resources> </struts-config> |
各項(xiàng)配置,可以參看這里,轉(zhuǎn):http://showmystage.javaeye.com/blog/183042
Register.java,處理注冊請求的Action:
import javax.servlet.http.*; import org.apache.struts.action.*; public class Register extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 如果失敗了,則由ActionForm返回 // 成功返回 return mapping.findForward("success"); } } |
這個(gè)Action只是在form信息通過驗(yàn)證后,返回成功信息“success”。成功信息的處理方式在“struts-config.xml”中配置。
RegFrom.java,它是注冊請求信息處理的校驗(yàn)類(ActionForm),就是常說的FormBean:
import java.text.ParseException; import java.text.SimpleDateFormat; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; public class RegForm extends ActionForm { private static final long serialVersionUID = 1L; private String username; private String password; private String password2; private String birthday; 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; } public String getPassword2() { return password2; } public void setPassword2(String password2) { this.password2 = password2; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public static long getSerialversionuid() { return serialVersionUID; } @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); // 校驗(yàn)用戶名 if (this.username == null || this.username.trim().equals("")) { ActionMessage message = new ActionMessage("errors.username.null"); errors.add("username", message); } // 校驗(yàn)密碼 if (this.password == null || this.password.trim().equals("")) { ActionMessage message = new ActionMessage("errors.password.null"); errors.add("password", message); } if(this.password2 == null || this.password2.trim().equals("")){ ActionMessage message = new ActionMessage("errors.password.null"); errors.add("password2", message); } if(!this.password.equals(this.password2)){ ActionMessage message = new ActionMessage("errors.password.diff"); errors.add("password2", message); } // 校驗(yàn)生日 if(this.birthday == null || this.birthday.equals("")){ ActionMessage message = new ActionMessage("errors.birthday.null"); errors.add("birthday", message); }else{ SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd"); try { df.parse(this.birthday); } catch (ParseException e) { ActionMessage message = new ActionMessage("errors.brithday.validate"); errors.add("birthday", message); e.printStackTrace(); } } return errors; } } |
它看起來更像一個(gè)Bean,它的成員名稱必須與form中的name屬性一致,否則會(huì)拋出異常。因?yàn)樗^承自ActionForm,并且在struts-config.xml中配置過。ActionServlet會(huì)自動(dòng)調(diào)用它的“validate”方法,校驗(yàn)form信息是否正確。如果errors為null或errors.size為0,AcitonServlet認(rèn)為form信息是成功通過校驗(yàn)的,并返回到Register(Aciton)中繼續(xù)處理。
上邊我們在Register中,如果校驗(yàn)成功直接返回“success”,但在實(shí)際開發(fā)中,我們還需要向數(shù)據(jù)庫中添加用戶的注冊信息。按照以往的做法,我們需要定義一個(gè)UserBean,并在Register類中,通過“execute”方法的“ActionForm form”參數(shù)獲取表單信息并填充到UserBean,然后交由業(yè)務(wù)邏輯處理模塊,將其添加到數(shù)據(jù)庫中。如果有用戶登錄功能,我們還需要在登陸的Action中填充這個(gè)UserBean。在以后的應(yīng)用中,我們可能需要在多處填充Bean,這種重復(fù)的工作,是需要改善的。
在此提出了BeanUtils,可以調(diào)用它的靜態(tài)方法“BeanUtils.copyProperties(user, form);”。user是UserBean的一個(gè)實(shí)例,form是execute的參數(shù)。只要一條語句就搞定了!在工程中添加一個(gè)User類,修改Register.java的ececute方法:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 如果失敗了,則由ActionForm返回 // 將form數(shù)據(jù)復(fù)制到User中 User user = new User(); BeanUtils.copyProperties(user, form); request.setAttribute("user", user); // 成功返回 return mapping.findForward("success"); } |
注意,struts框架是根據(jù)form的信息去填充user的。因此,user的成員名稱必須與form的成員名稱相同,且類型也必須相同。如果類型不同,必須編寫類型轉(zhuǎn)換器并使用“ConvertUtils.register”方法注冊到ConvertUtils中。比如,user中的birthday類型是java.util.Date,但form中的birthday是String類型。這時(shí)我們就需要編寫一個(gè)類型轉(zhuǎn)換器,類型轉(zhuǎn)換器最好在WEB應(yīng)用被初始化時(shí)注冊到ConvertUtils中,下面是在ServletContextListener的contextInitialized方法中注冊類型轉(zhuǎn)換器的:
@Override public void contextInitialized(ServletContextEvent arg0) { ConvertUtils.register(new Converter(){ @Override public Object convert(Class arg0, Object arg1) { if(arg0 != null){ DateFormat df = new SimpleDateFormat("yyyy-mm-dd"); try { return df.parse((String) arg1); } catch (ParseException e) { e.printStackTrace(); return new RuntimeException("錯(cuò)誤的日期時(shí)間格式!"); } }else{ throw new RuntimeException("請指定預(yù)轉(zhuǎn)換到的類信息!"); } } }, Date.class); } |
Ok,完成了!
接下來我們看一下頁面整個(gè)請求到響應(yīng)的過程,時(shí)間太晚了,我就不把老佟總結(jié)的進(jìn)行詳細(xì)的整理了,直接發(fā)上來吧:
1). *.do 根據(jù)web.xml,請求到 org.apache.struts.action.ActionServlet 2). ActionServlet中的doGet, doPost 調(diào)用 process() 方法, 再調(diào)用 Requestprocessor 的 process 方法. 3). ①. 獲取 servletpath: String path = processPath(request, response); ②. 根據(jù) path 獲取對應(yīng)的<action>節(jié)點(diǎn): ActionMapping mapping = processMapping(request, response, path); ③. 獲取對應(yīng)的 ActionForm 對象: ActionForm form = processActionForm(request, response, mapping); ④. 若配置類 action 的 validate 屬性為 true, 或使用默認(rèn)值則. 調(diào)用 ActionForm 的 validate() 方法進(jìn)行簡單驗(yàn)證: if (!processValidate(request, response, form, mapping)) { return; } 若驗(yàn)證沒有通過, 將頁面派發(fā)到 input 屬性指定的頁面. 方法結(jié)束, 此時(shí)請求不會(huì)到達(dá) Action (在配置文件struts-config.xml中進(jìn)行配置) ⑤. 若驗(yàn)證通過, 得到對應(yīng)的 Action 對象: Action action = processActionCreate(request, response, mapping); ⑥. 調(diào)用 action 的 execute() 方法 ⑦. 派發(fā)頁面. |
加油!