How does Spring know which view is to be returned? -



How does Spring know which view is to be returned? -

i'm new spring mvc , don't understand how spring knows must homecoming priceincrease.jsp, if isn't mapped in controller?

the other thing don't understand how spring auto-completes form action?

my controller is:

@controller @requestmapping(value="/priceincrease.html") public class priceincreaseformcontroller { /** logger class , 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()) { homecoming "priceincrease"; } int increment = priceincrease.getpercentage(); logger.info("increasing prices " + increment + "%."); productmanager.increaseprice(increase); homecoming "redirect:/hello.html"; } @requestmapping(method = requestmethod.get) protected priceincrease formbackingobject(httpservletrequest request) throws servletexception { priceincrease priceincrease = new priceincrease(); priceincrease.setpercentage(15); homecoming priceincrease; } public void setproductmanager(productmanager productmanager) { this.productmanager = productmanager; } public productmanager getproductmanager() { homecoming productmanager; } }

and jsp controller returns

<%@ 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: here 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 classpath of application @components deploy beans --> <context:component-scan base-package="com.mycompany.springapp.web" /> <!-- configures @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>

when dispatcherservlet initialized, viewresolver beans in applicationcontext. you've registered one

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

when handler method returns, spring looks @ homecoming value (and handler) determine do. in case, ie. returned string value, utilize viewnamemethodreturnvaluehandler indicates returned string value used view name.

once done, dispatcherservlet loop through viewresolver beans checking if view name can resolved view object. if can, utilize view, if can't seek next one. fail if can't resolve view name.

spring spring-mvc

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -