SpringMVC 搭建
来源:互联网 发布:淘宝飞鱼运动是正品吗 编辑:程序博客网 时间:2024/04/29 10:35
3、 减小Controler对页面地址的依赖
在上面的Controler中,return new ModelAndView("WEB-INF/jsp/hello.jsp", "now", now);指出了jsp页面的绝对路径,这可以改变。
首先在springapp-servlet.xml中增加一个bean定义:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
然后把Controler的返回写为:
return new ModelAndView("hello", "now", now);
这样InternalResourceViewResolver在返回页面之前,会自动加上prefix和suffix。
(。。。我不清楚这有什么用,如果我的路径除了/WEB-INF/jsp之外还有多个,那该怎么加呢?。。。看来只好都放到jsp下面了)
4、 bean定义中把其属性定义成其他的bean
在springapp-servlet.xml中,我们做出如下定义
<beans>
<bean id="springappController" class="web.SpringappController">
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<bean id="prodMan" class="bus.ProductManager">
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
</bean>
<bean id="product1" class="bus.Product">
<property name="description"><value>Lamp</value></property>
<property name="price"><value>5.75</value></property>
</bean>
<bean id="product2" class="bus.Product">
<property name="description"><value>Table</value></property>
<property name="price"><value>75.25</value></property>
</bean>
<bean id="product3" class="bus.Product">
<property name="description"><value>Chair</value></property>
<property name="price"><value>22.79</value></property>
</bean>
</beans>
有2点需要理解的:
1、 可以直接利用<property name="price"><value>75.25</value></property>为bean的属性赋值,也可以用<property name="productManager"> <ref bean="prodMan"/> </property>将属性赋为另一个定义的bean。
2、 这些被定义为属性的bean是在Web Module被加载时同时被加载的并且赋给属性的。一旦加载完成,就可以直接使用,因此没有必要在Class定义中为这些属性赋值(但必须有这些属性的setter定义)。比如按照上面的XML定义,我们把SpringappController写成这样:
public class SpringappController implements Controller {
private ProductManager prodMan;
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map myModel = new HashMap();
myModel.put("products", getProductManager().getProducts());
return new ModelAndView("hello", "model", myModel);
}
public void setProductManager(ProductManager pm) {
prodMan = pm;
}
public ProductManager getProductManager() {
return prodMan;
}
}
可以看到,没有为属性private ProductManager prodMan赋值,但定义了它的setter――setProductManager,这个setter在<bean id="springappController">被加载的时候自动调用了,以给prodMan赋上值。
5、 单元测试
在WEB应用开发中,我们习惯的测试方法是实际测试,即把WEB Module部署好了之后,用测试用例测试。这样效率很低,因为编译、打包、部署、测试的过程很费时间。
Spring的思想是及早测试以发现问题。
例子中,为测试SpringappController编写了测试程序:
package tests;
import java.util.Map;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import web.SpringappController;
import bus.ProductManager;
import bus.Product;
public class TestSpringappController extends TestCase {
private ApplicationContext ac;
public void setUp() throws IOException {
ac = new FileSystemXmlApplicationContext("src/tests/WEB-INF/springapp-servlet.xml");
}
public void testHandleRequest() throws ServletException, IOException {
SpringappController sc = (SpringappController) ac.getBean("springappController");
ModelAndView mav = sc.handleRequest((HttpServletRequest) null, (HttpServletResponse) null);
Map m = mav.getModel();
List pl = (List) ((Map) m.get("model")).get("products");
Product p1 = (Product) pl.get(0);
assertEquals("Lamp", p1.getDescription());
Product p2 = (Product) pl.get(1);
assertEquals("Table", p2.getDescription());
Product p3 = (Product) pl.get(2);
assertEquals("Chair", p3.getDescription());
}
}
测试类继承自junit.framework.TestCase。测试的时候,主要是测试controler产生的model对不对。当然在上面的例子中,测试没有多大的意义,因为model的产生太简单了。但是如果model产生的逻辑非常复杂,那这种测试是非常有意义的。
注意上面例子中的
public void setUp() throws IOException {
ac = new FileSystemXmlApplicationContext ( "src/tests/WEB-INF/springapp-servlet.xml" );
}
它为controler创建了一个类似于app server的环境(bean的加载就是在这里完成的)
转http://tb.blog.csdn.net/TrackBack.aspx?PostId=124982
6、 spring标签
为了使用spring标签,需要在web.xml中加入
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
这样就可以在JSP中使用spring标签了:
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%@ taglib prefix="spring" uri="/spring" %>
<html>
<head><title><fmt:message key="title"/></title></head>
<body>
<h1><fmt:message key="priceincrease.heading"/></h1>
<form method="post">
<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
<tr>
<td alignment="right" width="20%">Increase (%):</td>
<spring:bind path="priceIncrease.percentage">
<td width="20%">
<input type="text" name="percentage" value="<c:out value="${status.value}"/>">
</td>
<td width="60%">
<font color="red"><c:out value="${status.errorMessage}"/></font>
</td>
</spring:bind>
</tr>
</table>
<br>
<spring:hasBindErrors name="priceIncrease">
<b>Please fix all errors!</b>
</spring:hasBindErrors>
<br><br>
<input type="submit" alignment="center" value="Execute">
</form>
<a href="<c:url value="hello.htm"/>">Home</a>
</body>
</html>
7、 SimpleFormController的运作机制
前面说的Controller,比如SpringappController,它们只行使了页面流程控制和model的产生,都与页面的Form无关,也就是说不接受来自页面的提交数据。
为了处理页面的提交数据,那要让Controller继承自SimpleFormController。
通常的SimpleFormController定义如下:
public class PriceIncreaseFormController extends SimpleFormController {
private ProductManager prodMan;
public ModelAndView onSubmit(Object command)
throws ServletException {
int increase = ((PriceIncrease) command).getPercentage();
prodMan.increasePrice(increase);
String now = (new java.util.Date()).toString();
Map myModel = new HashMap();
myModel.put("now", now);
myModel.put("products", getProductManager().getProducts());
return new ModelAndView(new RedirectView(getSuccessView()));
}
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
PriceIncrease priceIncrease = new PriceIncrease();
priceIncrease.setPercentage(20);
return priceIncrease;
}
public void setProductManager(ProductManager pm) {
prodMan = pm;
}
public ProductManager getProductManager() {
return prodMan;
}
}
跟Controller的区别在于多了2个方法:
public ModelAndView onSubmit(Object command)
protected Object formBackingObject(HttpServletRequest request) throws ServletException
在理解SimpleFormController之前,我们先看看springapp-servlet.xml中关于SimpleFormController的定义:
<bean id="priceIncreaseForm" class="web.PriceIncreaseFormController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>priceIncrease</value></property>
<property name="commandClass"><value>bus.PriceIncrease</value></property>
<property name="validator"><ref bean="priceIncreaseValidator"/></property>
<property name="formView"><value>priceincrease</value></property>
<property name="successView"><value>hello.htm</value></property>
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
这些定义很好理解,其中这2行:
<property name="commandName"><value>priceIncrease</value></property>
<property name="commandClass"><value>bus.PriceIncrease</value></property>
指出了对应页面FORM的元素的类和其实例对象名。(我想不通的是为什么用command,而不是form,大概是认为提交就是命令吧)
springapp-servlet.xml中关于页面导向的定义如下:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
<prop key="/priceincrease.htm">priceIncreaseForm</prop>
</props>
</property>
</bean>
下面我们看看SimpleFormController的运作机制:
由于它也是一个Controller,所以它的加载机制同其他的Controller。比如也自动初始化了成员属性。
当页面请求交给PriceIncreaseFormController处理的时候,它首先调用的是formBackingObject()方法,其作用是加载页面Form元素对应的bean,并赋初始值。完成之后,就产生页面显示。
当用户提交FORM时,servlet不是先运行PriceIncreaseFormController. onSubmit(),而是先运行了priceIncreaseValidator的support()和validate (Object obj, Errors errors)方法。如果在校验过程中(即validate (Object obj, Errors errors)方法中),如果发现有数据错误,那么就errors.rejectValue()方法给errors赋值。〔〔errors.rejectValue()方法待研究〕〕
servlet一旦发现errors里面有值,就会中止程序运行而直接返回原来的页面,在页面的某处可以利用errors里面存储的值显示错误信息。
如果校验通过,那么SimpleFormController的onSubmit方法将启动,其返回值就是将要去的页面。
总结一个SimpleFormController的运作全过程:
servlet请求->SimpleFormController.formBackingObject()->显示页面->提交页面-> SimpleFormController的Validator.validate()->SimpleFormController. onSubmit()->view导向
- SpringMVC 搭建
- SpringMVC搭建
- SpringMVC搭建
- SpringMVC 搭建
- 搭建springmvc
- Springmvc搭建
- SpringMVC搭建
- springmvc搭建
- springMvc搭建
- 搭建SpringMVC
- SpringMVC搭建
- 搭建springMVC
- SpringMVC入门(搭建SpringMVC)
- 搭建springmvc工程
- 搭建springmvc工程
- SpringMVC的搭建
- SpringMVC环境搭建
- springMVC框架搭建
- 数据从sqlserver中导出到access数据库中
- 创业故事:牛粪里竟然淘出1000万?
- 消息处理的总结
- Windows环境Tomcat配置SSL
- 再谈明明白白用SATA硬盘
- SpringMVC 搭建
- Debian的字符编码
- JavaScript精度问题
- 使用mysqldump备份数据库
- Chapter 1 About the manuals
- 预实例化Bean
- 异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file
- Application context 和Resource 路径
- php fpdf 生成报表总结2(实例)