2014-10-03 83 views
0

我是Spring MVC的新手,我不明白Spring如何知道它必須返回priceincrease.jsp,如果它沒有映射到控制器中的話?Spring如何知道要返回哪個視圖?

我不明白的另一件事是彈簧如何自動完成窗體動作?

我的控制器:

@Controller 
@RequestMapping(value="/priceincrease.html") 
public class PriceIncreaseFormController { 

    /** Logger for this class and subclasses */ 
    protected final Log logger = LogFactory.getLog(getClass()); 

    @Autowired 
    private ProductManager productManager; 

    @RequestMapping(method = RequestMethod.POST) 
    public String onSubmit(@Valid PriceIncrease priceIncrease, BindingResult result) 
    { 
     if (result.hasErrors()) { 
      return "priceincrease"; 
     } 

     int increase = priceIncrease.getPercentage(); 
     logger.info("Increasing prices by " + increase + "%."); 

     productManager.increasePrice(increase); 

     return "redirect:/hello.html"; 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    protected PriceIncrease formBackingObject(HttpServletRequest request) throws ServletException { 
     PriceIncrease priceIncrease = new PriceIncrease(); 
     priceIncrease.setPercentage(15); 
     return priceIncrease; 
    } 

    public void setProductManager(ProductManager productManager) { 
     this.productManager = productManager; 
    } 

    public ProductManager getProductManager() { 
     return productManager; 
    } 

} 

這就是JSP控制器返回

<%@ include file="/WEB-INF/views/include.jsp" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 

<html> 
<head> 
    <title><fmt:message key="title"/></title> 
    <style> 
    .error { color: red; } 
    </style> 
</head> 
<body> 
    <h1><fmt:message key="priceincrease.heading"/></h1> 
    <form:form method="post" commandName="priceIncrease"> 
     <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"> 
      <tr> 
       <td align="right" width="20%">Increase (%):</td> 
       <td width="20%"> 
        <form:input path="percentage"/> 
       </td> 
       <td width="60%"> 
        <form:errors path="percentage" cssClass="error"/> 
       </td> 
      </tr> 
     </table> 
     <br> 
     <input type="submit" value="Execute"> 
    </form:form> 
    <a href="<c:url value="hello.html"/>">Home</a> 
</body> 
</html> 

UPDATE: 這裏是我的應用程序-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

     <bean id="productManager" class="com.mycompany.springapp.service.SimpleProductManager"> 
     <property name="products"> 
      <list> 
       <ref bean="product1"/> 
       <ref bean="product2"/> 
       <ref bean="product3"/> 
      </list> 
     </property> 
     </bean> 

     <bean id="product1" class="com.mycompany.springapp.domain.Product"> 
     <property name="description" value="Lamp"/> 
     <property name="price" value="5.75"/> 
     </bean> 

     <bean id="product2" class="com.mycompany.springapp.domain.Product"> 
     <property name="description" value="Table"/> 
     <property name="price" value="75.25"/> 
     </bean> 

     <bean id="product3" class="com.mycompany.springapp.domain.Product"> 
     <property name="description" value="Chair"/> 
     <property name="price" value="22.79"/> 
     </bean> 

     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename" value="messages"/> 
     </bean> 

     <!-- Scans the classpath of this application for @Components to deploy as beans --> 
     <context:component-scan base-package="com.mycompany.springapp.web" /> 

     <!-- Configures the @Controller programming model --> 
     <mvc:annotation-driven/> 

     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> 
     <property name="prefix" value="/WEB-INF/views/"></property> 
     <property name="suffix" value=".jsp"></property>   
     </bean> 
</beans> 
+0

什麼的'彈簧servlet.xml'?通常情況下,映射會告訴「jsp」文件的根目錄,並且要使用的「jsp」與控制器上的「@ RequestMapping」具有相同的路徑和名稱。 – Kevin 2014-10-03 22:28:11

+0

我已經添加了你問的xml。你能告訴spring如何知道它必須返回priceincrease.jsp而不是其他jsp? – Martin 2014-10-03 22:37:27

回答

0

DispatcherServlet是ini時tialized,它會在其ApplicationContext中尋找ViewResolver豆。您只註冊了一個

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> 
    <property name="prefix" value="/WEB-INF/views/"></property> 
    <property name="suffix" value=".jsp"></property>   
</bean> 

當您的處理程序方法返回時,Spring將查看返回值(和處理程序)以確定要執行的操作。在你的情況下,即。一個返回的String值,它將使用一個ViewNameMethodReturnValueHandler這表明返回的String值將被用作視圖名稱。

一旦完成,DispatcherServlet將遍歷ViewResolver bean,檢查視圖名稱是否可以解析爲View對象。如果可以的話,它會使用那個View,如果不行的話它會嘗試下一個。如果無法解析視圖名稱,它將會失敗。

+0

好吧,我明白,但formBackingObject方法返回一個PriceIncrease對象而不是一個字符串,春天如何將此對象解析爲priceincrease.jsp?它使用PriceIncrease類名嗎? – Martin 2014-10-03 22:56:27

+1

@Martin有一個不同的'HandlerMethodReturnValueHandler'來處理它。它被稱爲'ModelAttributeMethodProcessor',它根據您返回的對象的類型名稱確定一個視圖名稱。 – 2014-10-03 23:08:45

+0

非常感謝,現在我知道在哪裏看。 – Martin 2014-10-03 23:14:53

相關問題