Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
开发Spring MVC应用程序(3-4)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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




相关文章

相关软件