java - JSP with Spring form not calling Spring 4.x controller -
i spring newbie , have simple form on jsp:
<!-- register person. --> <p>to register library, please <i>click</i> button below:</p> <c:url value="/registerperson/register" var="url" /> <form:form commandname="person" action="${url}" method="get"> <input type="submit" value="register"> </form:form>
which want use talk spring controller:
@controller @requestmapping("/registerperson") @sessionattributes("person") public class registerpersoncontroller { private registerpersonvalidator registerpersonvalidator; private personservice personservice; @autowired public registerpersoncontroller(registerpersonvalidator registerpersonvalidator, personservice personservice) { this.registerpersonvalidator = registerpersonvalidator; this.personservice = personservice; } // populates registerpersonform dummy person when new person registering library. @requestmapping(method = requestmethod.get) public string getregisterperson(model model) { system.out.println("hello"); person person = new person(); model.addattribute("person", person); return "registerperson"; }
but when try form, tomcat 404 page not found error.
the registerperson
view stored in \web\web-inf\jsp
jsp.
my dispatcherservlet
is:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="library.controller" /> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix"> <value>/web-inf/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>
and web.xml
:
<display-name>library</display-name> <servlet> <servlet-name>library</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>library</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/library-servlet.xml</param-value> </context-param> <welcome-file-list> <welcome-file>/web-inf/jsp/index.jsp</welcome-file> </welcome-file-list>
can advise why controller not called when try url:
http://localhost:8080/library/registerperson
using get?
change library library, urls mapped servlet name, not display name.
you have no controller url
<c:url value="/registerperson/register" var="url" />
you have shown "/registerperson".
Comments
Post a Comment