Spring如何知道要返回哪个视图?

时间:2014-10-03 22:14:11

标签: spring spring-mvc

我是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>

更新 这是我的app-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>

2 个答案:

答案 0 :(得分:0)

初始化DispatcherServlet时,它会在ViewResolver中查找ApplicationContext个bean。你只注册了一个

<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,如果不能,它将尝试下一个。如果无法解析视图名称,则会失败。

答案 1 :(得分:0)

我一直在做一些研究,我找到了两个问题之一的答案: Spring如何知道要返回哪个视图?

Spring Framework参考文档 - 17.13.3 View - RequestToViewNameTranslator http://docs.spring.io/spring/docs/4.1.2.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#mvc-coc-r2vnt