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

Spring如何知道要返回哪个视图?,spring,spring-mvc,Spring,Spring Mvc,我是SpringMVC的新手,我不明白Spring如何知道它必须返回priceincrease.jsp,如果它没有映射到控制器中的话 另一件我不明白的事情是spring auto是如何完成表单操作的 我的控制器是: @Controller @RequestMapping(value="/priceincrease.html") public class PriceIncreaseFormController { /** Logger for this class and subclas

我是SpringMVC的新手,我不明白Spring如何知道它必须返回priceincrease.jsp,如果它没有映射到控制器中的话

另一件我不明白的事情是spring auto是如何完成表单操作的

我的控制器是:

@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>

初始化
DispatcherServlet
时,它将在其
应用程序上下文中查找
ViewResolver
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会查看返回值(和处理程序)以确定要执行的操作。在您的情况下,即返回的
字符串
值,它将使用
ViewNameMethodReturnValueHandler
,这表示返回的
字符串
值将用作视图名称


完成后,
DispatcherServlet
将通过
viewsolver
bean循环检查视图名称是否可以解析为
view
对象。如果可以,它将使用该视图,如果不能,它将尝试下一个视图。如果无法解析视图名称,它将失败

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

Spring框架参考文档-17.13.3视图-RequestToViewNameTranslator

SpringServlet.xml中有什么?通常,那里的映射告诉
jsp
文件的根,要使用的
jsp
与控制器上的
@RequestMapping
具有相同的路径和名称。我已经添加了您要求的xml。您能告诉我spring是如何知道它必须返回priceincrease.jsp而不是其他jsp的吗?好的,我理解,但是formBackingObject方法返回的是priceincrease对象而不是字符串,spring是如何将这个对象解析为priceincrease.jsp的?它使用PriceIncrease类名吗?@Martin有一个不同的
HandlerMethodReturnValueHandler
来处理这个问题。它被称为
ModelAttributeMethodProcessor
,它根据您返回的对象的类型名称确定视图名称。