hibernate - Need help on Spring MVC hello world -
i trying learn spring mvc on netbeans 8.0.2 using hibernate , frankly stuck. appreciated. trying simple "hello world" type site.
someone on first page hit submit button , resulting page have list of values db. sounds pretty simple right? i've included 5 short files here me if inclined. "web.xml", "dispatcher-servlet.xml","index.jsp", "teamcontroller.java", "secondview.jsp"
the way understand how spring mvc should work, using files, follows...
1) running project netbeans, index.jsp file brought up. happens because web.xml consulted, has following line...
"<welcome-file>redirect.jsp</welcome-file>"
once redirect.jsp consulted, see has following...
"<% response.sendredirect("index.htm"); %>"
so redirect go web.xml has following...
"<servlet-name>dispatcher</servlet-name>" "<url-pattern>*.htm</url-pattern>"
so request coming in index.htm handled dispatcher servlet. in dispatcher servlet's config file, view resolver add following... p:prefix="/web-inf/jsp/" p:suffix=".jsp" /> index.htm gets changed /wen-inf/jsp/index.jsp , page brought up.
2) @ point index.jsp brought up. thing in file form submit button. intent have press button , info db returned on screen. have hibernate part of web project , have created java class "team.java" based on db table "team". populated few records. seeing once index.jsp comes , hit submit, giving 404.
this url shown form "host:port/hellowebfour/index.htm " if view source shows index.jsp. when hit submit gives 404 url "host:port/hellowebfour/team? " btw, "hellowebfour" name of project in netbeans. not sure happening, if understanding right or if need add anything. appreciated... short code files below...
"web.xml"
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/applicationcontext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app>
"dispatcher-servlet.xml"
<?xml version='1.0' encoding='utf-8' ?> <!-- was: <?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" xmlns:context="http://www.springframework.org/schema/context"> <context:component-scan base-package="testnew1" /> <bean class="org.springframework.web.servlet.mvc.support.controllerclassnamehandlermapping"/> <!-- controllers use controllerclassnamehandlermapping above, index controller using parameterizableviewcontroller, must define explicit mapping it. --> <bean id="urlmapping" class="org.springframework.web.servlet.handler.simpleurlhandlermapping"> <property name="mappings"> <props> <prop key="index.htm">indexcontroller</prop> </props> </property> </bean> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver" p:prefix="/web-inf/jsp/" p:suffix=".jsp" /> <!-- index controller. --> <bean name="indexcontroller" class="org.springframework.web.servlet.mvc.parameterizableviewcontroller" p:viewname="index" /> </beans>
"index.jsp"
<%@page contenttype="text/html" pageencoding="utf-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>index.jsp - submit page</title> </head> <body> <h2>hit submit db information</h2> <form:form method="get" action="/hellowebfour/team"> <table> <tr> <td> <input type="submit" value="submit"/> </td> </tr> </table> </form:form> </body> </html>
"teamcontroller.java"
package testnew1; import org.springframework.stereotype.controller; import org.springframework.ui.modelmap; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.servlet.modelandview; @controller public class teamcontroller { @requestmapping(value = "/team", method = requestmethod.get) public string getteam(@modelattribute("springweb") team team, modelmap model) { model.addattribute("city", team.getcity()); model.addattribute("state", team.getstate()); model.addattribute("nickname", team.getnickname()); return "secondview"; } }
"secondview.jsp"
<%@page contenttype="text/html" pageencoding="utf-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>secondview.jsp - results page</title> </head> <body> <h2>database information</h2> <table> <tr> <td>city</td> <td>${city}</td> </tr> <tr> <td>state</td> <td>${state}</td> </tr> <tr> <td>nickname</td> <td>${nickname}</td> </tr> </table> </body> </html>
as far see submiting "/hellowebfour/team
request handler method mapped /team
, try submit /team
. don't use prefix on dispatcher mapping can missed .htm
on url combining unknown request url /hellowebfour/team
.
then if 1 of above solutions work, method may fail because expecting modelattribute named "springweb" of type team, form dont post data , it's modelattribute mapped "command", default value, removing can possibly remove further error if occurs.
Comments
Post a Comment