介绍完配置文件过后,具体看看项目的MVC和DAO层
修改web.xml文件
<servlet-mapping>
<!--
<servlet-name>petstore</servlet-name>
-->
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
这里先使用struts的action,先不用spring的MVC,映射配置文件是struts-config.xml
首先看看用户注册页面struts目录下NewAccountForm.jsp,这里使用的是jstl标签库
<%@ include file="IncludeAccountFields.jsp" %>包含了用户详细信息
提交的action是/shop/newAccount.do
察看struts-config.xml
<action path="/shop/newAccount"
type="org.springframework.samples.jpetstore.web.struts.NewAccountAction"
name="workingAccountForm" scope="session" validate="true"
input="/WEB-INF/jsp/struts/NewAccountForm.jsp">
<forward name="success" path="/shop/index.do"/>
</action>
处理类是org.springframework.samples.jpetstore.web.struts.NewAccountAction
FORM是workingAccountForm
scope="session" 作用域为session
validate="true" 使用校验,调用form中的validate()方法
NewAccountAction的源码:
package org.springframework.samples.jpetstore.web.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.samples.jpetstore.domain.Account;

public class NewAccountAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
AccountActionForm acctForm = (AccountActionForm) form;
if (AccountActionForm.VALIDATE_NEW_ACCOUNT.equals(acctForm.getValidate())) {
acctForm.getAccount().setListOption(request.getParameter("account.listOption") != null);
acctForm.getAccount().setBannerOption(request.getParameter("account.bannerOption") != null);
Account account = acctForm.getAccount();
String username = acctForm.getAccount().getUsername();
getPetStore().insertAccount(account);
acctForm.setAccount(getPetStore().getAccount(username));
PagedListHolder myList = new PagedListHolder(getPetStore().getProductListByCategory
(account.getFavouriteCategoryId()));
myList.setPageSize(4);
acctForm.setMyList(myList);
request.getSession().setAttribute("accountForm", acctForm);
request.getSession().removeAttribute("workingAccountForm");
return mapping.findForward("success");
}
else {
request.setAttribute("message", "Your account was not created because the submitted
information was not validated.");
return mapping.findForward("failure");
}
}
}
一句一句来-------------------
1>
AccountActionForm继承BaseAction
BaseAction中:
public abstract class BaseAction extends Action {

private PetStoreFacade petStore;

public void setServlet(ActionServlet actionServlet) {
super.setServlet(actionServlet);
if (actionServlet != null) {
ServletContext servletContext = actionServlet.getServletContext();
WebApplicationContext wac =
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
this.petStore = (PetStoreFacade) wac.getBean("petStore");
}
}

protected PetStoreFacade getPetStore() {
return petStore;
}

}
继承Action类,action每次初始化时自动调用setServlet方法,这里将spring管理的bean赋给action
ServletContext servletContext = actionServlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
ApplicationContext可以看作为一个应用容器,org.springframework.web.context.ContextLoaderListener在web启动时初始化这个容器,将bean放在这个容器中
WebApplicationContext继承自ApplicationContext,Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了
得到WebApplicationContext实例,可以用getBean方法来获取bean
2>
AccountActionForm acctForm = (AccountActionForm) form;
实例化AccountActionForm,它继承与BaseActionForm
BaseActionForm类继承org.apache.struts.action.ActionForm,重写了父类的validate方法,用于校验处理
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors actionErrors = null;
ArrayList errorList = new ArrayList();
doValidate(mapping, request, errorList);
request.setAttribute("errors", errorList);
if (!errorList.isEmpty()) {
actionErrors = new ActionErrors();
actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("global.error"));
}
return actionErrors;
}
再回到AccountActionForm,只要继承BaseActionForm,然后重写doValidate(mapping, request, errorList)方法,就可以自由定义校验规则.
修改web.xml文件








首先看看用户注册页面struts目录下NewAccountForm.jsp,这里使用的是jstl标签库
<%@ include file="IncludeAccountFields.jsp" %>包含了用户详细信息
提交的action是/shop/newAccount.do
察看struts-config.xml

type="org.springframework.samples.jpetstore.web.struts.NewAccountAction"

input="/WEB-INF/jsp/struts/NewAccountForm.jsp">


FORM是workingAccountForm
scope="session" 作用域为session
validate="true" 使用校验,调用form中的validate()方法
NewAccountAction的源码:












HttpServletRequest request, HttpServletResponse response) throws Exception {










(account.getFavouriteCategoryId()));








information was not validated.");




一句一句来-------------------
1>
AccountActionForm继承BaseAction
BaseAction中:



















ServletContext servletContext = actionServlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
ApplicationContext可以看作为一个应用容器,org.springframework.web.context.ContextLoaderListener在web启动时初始化这个容器,将bean放在这个容器中
WebApplicationContext继承自ApplicationContext,Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了
得到WebApplicationContext实例,可以用getBean方法来获取bean
2>
AccountActionForm acctForm = (AccountActionForm) form;
实例化AccountActionForm,它继承与BaseActionForm
BaseActionForm类继承org.apache.struts.action.ActionForm,重写了父类的validate方法,用于校验处理










