`
wutao8818
  • 浏览: 612634 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

重头开始学 Spring JPetStore 5

阅读更多
重头开始学 Spring JPetStore 4 里面的Controller似乎太简单了一些。

让我们看一个更复杂一些的请求

看看注册页面

引用
http://localhost:8080/jpetstore/shop/newAccount.do


这里有一个表单需要提交。沿着这个请求 /shop/newAccount.do 找到对应的bean

引用
<bean name="/shop/newAccount.do" class="org.springframework.samples.jpetstore.web.spring.AccountFormController">
<property name="petStore" ref="petStore"/>
<property name="validator" ref="accountValidator"/>
<property name="successView" value="index"/>
</bean>


我们可以看到这个控制器变成了AccountFormController。继续SimpleFormController 看看文档我们发现这个控制器就是一个为表单而生的。另外注入了一个petStore ,看看代码,我们就知道这是一个真实的业务层处理的类。还有一个验证器 :accountValidator

但是这个验证在哪?在另一个文件 applicationContext.xml 里。

引用
<!-- Generic validator for Account objects, to be used for example by the Spring web tier -->
<bean id="accountValidator" class="org.springframework.samples.jpetstore.domain.logic.AccountValidator"/>


以及 successView 一个view页面指向。

看看这个控制器最主要的方法吧。

引用

protected ModelAndView onSubmit(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {

AccountForm accountForm = (AccountForm) command;
try {
if (accountForm.isNewAccount()) {
this.petStore.insertAccount(accountForm.getAccount());
}
else {
this.petStore.updateAccount(accountForm.getAccount());
}
}
catch (DataIntegrityViolationException ex) {
errors.rejectValue("account.username", "USER_ID_ALREADY_EXISTS",
"User ID already exists: choose a different ID.");
return showForm(request, response, errors);
}

UserSession userSession = new UserSession(this.petStore.getAccount(accountForm.getAccount().getUsername()));
PagedListHolder myList = new PagedListHolder(
this.petStore.getProductListByCategory(accountForm.getAccount().getFavouriteCategoryId()));
myList.setPageSize(4);
userSession.setMyList(myList);
request.getSession().setAttribute("userSession", userSession);
return super.onSubmit(request, response, command, errors);
}


有一点让人愉快的是这里的AccountForm似乎不需要像在struts中一样特别麻烦的配置一次。只要一句话就可以了,省力了。
引用

AccountForm accountForm = (AccountForm) command;



引用
if (accountForm.isNewAccount()) {
this.petStore.insertAccount(accountForm.getAccount());
}
else {
this.petStore.updateAccount(accountForm.getAccount());
}


保存的业务逻辑被petStore包起来了,所以直接调用就结束了。关键是异常处理

引用
catch (DataIntegrityViolationException ex) {
errors.rejectValue("account.username", "USER_ID_ALREADY_EXISTS",
"User ID already exists: choose a different ID.");
return showForm(request, response, errors);
}


这里处理了一个重复用户名的异常。errors. rejectValue方法带了好几个参数。应该是国际化资源文件里的key,以及默认提示信息等内容,然后呢,然后调用了
引用
showForm(request, response, errors);

这个方法应该是一个父类的方法。自动回填了表单的提交的数据?自己试试提交一个重复的用户名,页面是不是回填了?我试验了一下是这样的,完成的还算不错。

看看它是怎么实现这个工作的呢?这个页面到底在哪?让我们看看。

引用
public AccountFormController() {
setSessionForm(true);
setValidateOnBinding(false);
setCommandName("accountForm");
setFormView("EditAccountForm");
}


其中有一个 setFormView("EditAccountForm"); 知道了吧。就是EditAccountForm.jsp了。

找到它,看看。

页面开始就是这么一段。似乎是所有的accountForm的属性都被绑定到status这个变量了现在我还不确定。先留下这个问号?接下去再看看。起码我们通过el标签可以看出来status有个属性 errorMessages 。这个集合保存了所有的错误。JSTL c:forEach标签遍历了显示了所有的错误。
引用


<!-- Support for Spring errors object -->
<spring:bind path="accountForm.*">
<c:forEach var="error" items="${status.errorMessages}">
<B><FONT color=RED> <BR> <c:out value="${error}" /> </FONT>
</B>
</c:forEach>
</spring:bind>



随便看一个input
引用


<tr bgcolor="#FFFF88">
<td>
New password:
</td>
<td>
<spring:bind path="accountForm.account.password">
<input type="password"
name="<c:out value="${status.expression}"/>"
value="<c:out value="${status.value}"/>" />
</spring:bind>
</td>
</tr>



总是被一个<spring:bind 标签包裹着。name属性也被${status.expression}动态生成了。这样做似乎于前面提到的一个方便有关。 不需要像在struts中一样特别麻烦的配置一次AccountForm。只要一句话就可以了 猜得不错的话,应该是与这个很大的关系。

看到这个页面不怎么好。满页面的<spring:bind ,每个input都要。还真是烦。


这个控制器里面有一个方法 referenceData
Create a reference data map for the given request.

在AbstractFormController里面可以找到这个方法如何被调用的。合并到一个MAP里面了。

接下来干嘛了?
引用


UserSession userSession = new UserSession(this.petStore.getAccount(accountForm.getAccount().getUsername()));
PagedListHolder myList = new PagedListHolder(
this.petStore.getProductListByCategory(accountForm.getAccount().getFavouriteCategoryId()));
myList.setPageSize(4);
userSession.setMyList(myList);
request.getSession().setAttribute("userSession", userSession);
return super.onSubmit(request, response, command, errors);



引用
PagedListHolder
似乎是一个分页的类。没看到这个list在index.jsp页面有调用。他在哪里?不知道。


经过这个请求的跟踪,我发现spring mvc真的太灵活。以至于我都不知道什么地方该写哪些?没人告诉我必须实现
引用


public AccountFormController() {
setSessionForm(true);
setValidateOnBinding(false);
setCommandName("accountForm");
setFormView("EditAccountForm");
}



里面的4个方法。如果我没写setFormView("EditAccountForm");应该不行。但是没有一个机制让我知道哪些是必须的。在这样的系统上写代码是不是有点走钢丝的感觉?
从代码的角度来看,它这样的实现无可厚非,但似乎对使用者不够友好 。












分享到:
评论

相关推荐

    学习Spring 的例子JpetStore

    《Spring框架学习:以JpetStore为例》 Spring框架是Java企业级应用开发中的核心框架,它为开发者提供了丰富的功能,简化了开发流程,提高了代码的可测试性和可维护性。JpetStore作为Spring的经典示例项目,是学习...

    spring例子: jpetstore

    5. **Spring Boot**:现代Spring开发的首选方式,它简化了Spring应用的初始设置和配置,内置了Tomcat服务器,支持自动配置。 6. **Spring Data JPA**:Spring的数据访问抽象层,用于简化JPA(Java Persistence API...

    spring jpetstore spring附带的例子

    《Spring JPetStore:Spring框架的经典示例》 Spring框架是Java开发中广泛使用的轻量级框架,以其灵活、高效和模块化的特性深受开发者喜爱。其中,JPetStore项目是Spring官方提供的一个经典示例,它展示了Spring的...

    spring jpetstore2.5

    《Spring JPetStore 2.5:MyEclipse下的实战指南》 在IT行业中,Spring框架作为Java企业级应用开发的主流选择,以其强大的功能和灵活性赢得了广大开发者喜爱。而JPetStore作为Spring官方提供的一个示例应用,是学习...

    Spring jpetstore

    **Spring jpetstore** 是一个基于Spring框架的开源示例应用,它展示了如何使用Spring MVC、Spring JDBC以及Spring的其他核心特性来构建一个完整的Web应用。这个项目是Spring官方提供的,旨在帮助开发者学习和理解...

    jpetstore4.0 (spring+struts+ibatis)

    《基于Spring、Struts和iBatis的jpetstore4.0详解》 jpetstore4.0是一款经典的电子商务示例应用,它采用Spring、Struts和iBatis这三个核心框架构建,展示了如何在Java环境下实现一个完整的MVC(Model-View-...

    jpetstore spring 的经典完整可直接运行的例子 jpetstore

    《基于jpetstore的Spring、iBatis与Struts整合实战》 jpetstore项目是Spring框架的一个经典示例,它全面展示了如何将Spring、iBatis和Struts这三个核心的Java Web技术进行集成,构建出一个完整的MVC(Model-View-...

    MyEclipse中加载Spring的JPetStore

    《MyEclipse中加载Spring的JPetStore详解》 在软件开发领域,Spring框架以其强大的功能和灵活性,已经成为Java企业级应用开发的事实标准。而MyEclipse作为一款强大的Java集成开发环境,为开发者提供了便捷的Spring...

    spring之jpetstore

    《Spring之JPetStore:深度解析与实践指南》 在Java世界中,Spring框架以其强大的功能和灵活性,已经成为企业级应用开发的首选。而JPetStore作为Spring框架的经典示例项目,是学习和理解Spring核心特性的绝佳起点。...

    spring的jpetstore工程(myeclipse)

    《Spring的JPetStore工程与MyEclipse集成详解》 Spring框架是Java开发中的核心组件,它提供了丰富的功能,如依赖注入(DI)、面向切面编程(AOP)、数据访问和事务管理等。JPetStore作为Spring官方提供的一个示例...

    Spring+jpetstore+Myeclipse

    最新spring带的JPetStore的MyEclipse项目,包括了数据库,可用hsqldb直接运行,可以直接导入MyEclipse中并部署运行。 在Myeclipse里新建一个web项目,导入shopping项目即可,数据库在db文件夹里

    eclipse_spring_jpetstore.rar

    5. `WEB-INF/spring`:存放Spring的配置文件,如`applicationContext.xml`定义了bean的实例化、依赖关系等。 五、Spring核心概念实践 1. 依赖注入:通过XML配置或注解方式实现,例如在`applicationContext.xml`中...

    Spring源码学习-JPetStore.part3

    spring自带的JPetStore,我已经配置好(数据库也配置好,用的是hsqldb),可以直接导 入eclipse中运行。共3个压缩包

    JPetStore5(更正了其中的2个错误)

    **JPetStore5:一个基于J2EE的开源电子商务示例** **简介** JPetStore5是基于Java 2 Enterprise Edition (J2EE) 平台的一个经典示例应用,它是一个在线宠物商店,用于展示如何实现电子商务功能。这个项目在原有的...

    JPetStore (Struts + Spring + Hibernate)版

    **标题解析:** "JPetStore (Struts + Spring + Hibernate)版" 是一个基于Java技术的开源电子商务示例应用,它集成了Struts、Spring和Hibernate三个关键的开源框架。这个版本相较于之前的JPetStore5.0,进行了重要的...

    Struts+Spring+Hibernate实现的jpetstore

    在 jpetstore 中,Spring 作为整个应用的“胶水”,管理各个组件的生命周期和依赖关系。它可能通过XML配置或注解驱动的方式来定义bean,这些bean可以是DAO(数据访问对象)、Service、Controller等。Spring还提供了...

    JPetStore-5.0.zip_JPETSTO_jpetstore-5_jpetstore5_jpetstore5.0_jp

    总的来说,JPetStore-5.0不仅是一个电商应用实例,也是一个学习和研究Java Web开发、iBatis、Spring和MVC模式的宝贵资源。通过对这个项目的深入研究,开发者可以提升自己的技术能力,更好地理解和运用相关技术栈。

    jpetstore开源学习代码

    5. **JNDI(Java Naming and Directory Interface)**:JNDI是Java的命名和目录接口,允许应用程序查找和使用服务,如数据库连接池。在jpetstore中,可能利用JNDI查找和管理EJB或者数据源。 6. **JTA(Java ...

    jpetstore

    该jpetstore经典案例为最新SPRING开发包里面的完全案例,并集成到eclipse里面了,在eclipse里面可以直接运行并调试,在工作目录里面直接建立jpetstore目录,自动导入该目录下文件,修改jdbc.properties配置文件,连接...

Global site tag (gtag.js) - Google Analytics