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

类别:Java 点击:0 评论:0 推荐:

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         重新部署程序,就可以测试你的程序了

本文地址:http://com.8s8s.com/it/it16192.htm