开发Spring MVC应用程序(3-4)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

l priceIncreaseForm Bean定义表单对应的控制器:

Ø sessionForm:是否启用session

Ø commandName:Command对象名,在Spring标记中引用

Ø commandClass:Command对象的类全路径

Ø validator:验证器类全路径

Ø formView:表单视图页面

Ø successView:表单提交成功后显示的视图页面(译者:这里不能使用逻辑名字)

l 表单控制器扩展SimpleFormController,主要处理是onSubmit()方法,其参数是指定的Command对象,以便获得表单提交的数据,然后调用ProductManager的increasePrice()方法增加价格,最后重定向到successView

package web;

import org.springframework.web.servlet.mvc.SimpleFormController;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

import java.util.HashMap;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import bus.ProductManager;

import bus.PriceIncrease;

public class PriceIncreaseFormController extends SimpleFormController {

/** Logger for this class and subclasses */

protected final Log logger = LogFactory.getLog(getClass());

private ProductManager prodMan;

public ModelAndView onSubmit(Object command)

throws ServletException {

int increase = ((PriceIncrease) command).getPercentage();

logger.info("Increasing prices by " + increase + "%.");

prodMan.increasePrice(increase);

String now = (new java.util.Date()).toString();

logger.info("returning from PriceIncreaseForm view to " + getSuccessView() +

" with " + now);

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;

}

}

l formBackingObject()方法是回传数据到表单,最后返回的是Command对象

l 在messages.properties中,加入本例使用的一些消息:

title=SpringApp

heading=Hello :: SpringApp

greeting=Greetings, it is now

priceincrease.heading=Price Increase :: SpringApp

error.not-specified=Percentage not specified!!!

error.too-low=You have to specify a percentage higher than {0}!

error.too-high=Don't be greedy - you can't raise prices by more than {0}%!

required=Entry required.

typeMismatch=Invalid data.

typeMismatch.percentage=That is not a number!!!

l 同时也在hello.jsp中添加到priceincrease.jsp的链接

<%@ include file="/WEB-INF/jsp/include.jsp" %>

<html>

<head><title><fmt:message key="title"/></title></head>

<body>

<h1><fmt:message key="heading"/></h1>

<p><fmt:message key="greeting"/> <c:out value="${model.now}"/>

</p>

<h3>Products</h3>

<c:forEach items="${model.products}" var="prod">

<c:out value="${prod.description}"/> <i>$<c:out value="${prod.price}"/></i><br><br>

</c:forEach>

<br>

<a href="<c:url value="priceincrease.htm"/>">Increase Prices</a>

<br>

</body>

</html>

l 重新部署程序,就可以测试你的程序了

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航