jsp - Handle two URL pattern form one controller in Spring MVC -
i'm learning spring mvc using spring tool suite in eclipse ide.my base package org.springtest.test , i've create 2 jsp pages under view folder index.jsp , home.jsp. i'm trying use home.jsp welcome page. when i'm trying access http://localhost:8081/test/ , http://localhost:8081/test/index.jsp gives index.jsp page.i tried add home page in welcome file-list.but same result.please web.xml , controller class.how can fix this?.
homecontroller.java
package org.springtest.test; import java.text.dateformat; import java.util.date; import java.util.locale; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; @controller public class homecontroller { private static final logger logger = loggerfactory .getlogger(homecontroller.class); @requestmapping(value = "/", method = requestmethod.get) public string home(locale locale, model model) { logger.info("welcome home! client locale {}.", locale); date date = new date(); dateformat dateformat = dateformat.getdatetimeinstance(dateformat.long, dateformat.long, locale); string formatteddate = dateformat.format(date); model.addattribute("servertime", formatteddate); return "home"; } @requestmapping(value = "index", method = requestmethod.get) public string home1(locale locale, model model) { logger.info("welcome index! client locale {}.", locale); date date = new date(); dateformat dateformat = dateformat.getdatetimeinstance(dateformat.long, dateformat.long, locale); string formatteddate = dateformat.format(date); model.addattribute("servertime", formatteddate); return "index"; } }
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring/root-context.xml</param-value> </context-param> <!-- creates spring container shared servlets , filters --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- processes application requests --> <servlet> <servlet-name>appservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring/appservlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appservlet</servlet-name> <url-pattern>/</url-pattern> <url-pattern>/index.jsp</url-pattern> </servlet-mapping> </web-app>
for spring-webmvc need register "internalresourceviewresolver" bean if not yet done. if done can please attache configuration?
the internalresourceviewresolver used match return-code "home" , "index" controller-methods cetrain view (jsp).
a resource view resolver can configured this:
@bean public internalresourceviewresolver jspviewresolver() { internalresourceviewresolver bean = new internalresourceviewresolver(); bean.setprefix("/web-inf/views/"); bean.setsuffix(".jsp"); return bean; }
it take string-name returned controller; e.g. "home" , append ".jsp" , "/web.inf/views/home.jsp". if can not found error thrown or welcome-jsp delvivered.
i've attached servlet-context.xml , console out put also.
servlet-context.xml
<beans:bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <beans:property name="prefix" value="/web-inf/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="org.springtest.test" />
last lines of console out put
info : org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping - mapped "{[/],methods=[get],params=[],headers=[],consumes=[],produces= [],custom=[]}" onto public java.lang.string org.springtest.test.homecontroller.home (java.util.locale,org.springframework.ui.model) info : org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping - mapped "{[/index],methods=[get],params=[],headers=[],consumes= [],produces=[],custom=[]}"onto public java.lang.string org.springtest.test.homecontroller.home1(java.util.locale,org.springframework.ui.model) info : org.springframework.web.servlet.handler.simpleurlhandlermapping - mapped url path [/resources/**] onto handler 'org.springframework.web.servlet.resource.resourcehttprequesthandler#0' info : org.springframework.web.servlet.dispatcherservlet - frameworkservlet 'appservlet': initialization completed in 544 ms info : org.springtest.test.homecontroller - welcome index! client locale en_us.
Comments
Post a Comment