spring JpetStore学习笔记(四)

介绍完配置文件过后,具体看看项目的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)方法,就可以自由定义校验规则.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值