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

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

Ø         sessionForm:是否启用session

Ø         commandNameCommand对象名,在Spring标记中引用

Ø         commandClassCommand对象的类全路径

Ø         validator:验证器类全路径

Ø         formView:表单视图页面

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

l         表单控制器扩展SimpleFormController主要处理是onSubmit()方法,其参数是指定的Command对象,以便获得表单提交的数据,然后调用ProductManagerincreasePrice()方法增加价格,最后重定向到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         重新部署程序,就可以测试你的程序了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值