java - Two very similar SpringMVC projects. One fails to display model data -
i've made 2 projects, both basic spring mvc basic configurations , single controller. of files in both projects identical controller, view , view-resolver definition. done purpose of finding causing failure: in projecta view fails display model data sent controller. projectb works fine. (i using spring 4.3.9 , tomcat 8.5.20)
project a - dispatcher servlet defined extending abstractannotationconfigdispatcherservletinitializer
. getrootconfigclasses()
returns config class links web-config.xml define view resolver.
├── pom.xml └── src └── main ├── java │ └── mywebapp │ ├── config │ │ ├── mywebappinitializer.java │ │ └── rootconfig.java │ └── web │ └── homecontroller.java ├── resources │ └── web-config.xml └── webapp └── web-inf ├── views │ └── home.jsp └── web.xml
project b - dispatcher defined in web.xml. servlet-context , root-context linked in web.xml.
├── pom.xml └── src └── main ├── java │ └── mywebapp │ └── web │ └── homecontroller.java ├── resources │ ├── app-config.xml │ └── web-config.xml └── webapp └── web-inf ├── views │ └── home.jsp └── web.xml
project a returns
hello world! ${message} ${message}
project b returns
hello world! message in model message in model
since both projects use identical:
a) jsp view. b) controller said view. c) config file defining view resolver.(web-config.xml)
and root-context files (app-config.xml , rootconfig.java) serve links between servlet-context files , dispatcher definition, cause of failure must come java-config of dispatcher servlet. can't find it.
mywebappinitializer.java project a
package mywebapp.config; import org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer; public class mywebappinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected string[] getservletmappings() { return new string[] { "/" }; } @override protected class<?>[] getrootconfigclasses() { return new class<?>[] { rootconfig.class }; } @override protected class<?>[] getservletconfigclasses() { return null; } }
web.xml project b
<web-app 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" version="3.1"> <display-name>spring mvc xml configuration example</display-name> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:app-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>my-dispatcher-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:web-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>my-dispatcher-servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
web-config.xml - shared both projects
<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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package="mywebapp.web" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="viewclass" value="org.springframework.web.servlet.view.jstlview"/> <property name="prefix" value="/web-inf/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
homecontroller.java - shared both projects
package mywebapp.web; import org.springframework.stereotype.controller; import org.springframework.ui.modelmap; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; @controller public class homecontroller { @requestmapping(value = "/", method = requestmethod.get) public string index(modelmap model){ model.addattribute("message", "my message in model"); return "home"; } }
please me find cause failure display model data can have working java-config template.
rootconfig.java project a
package mywebapp.config; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.componentscan.filter; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.filtertype; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.context.annotation.importresource; import java.util.list; @configuration @componentscan (basepackages={"mywebapp"}, excludefilters={@filter(type=filtertype.annotation, value=enablewebmvc.class)}) @importresource("classpath:web-config.xml") public class rootconfig { }
step 1) in project a - delete web.xml
created maven archetype.
step 2) add following pom.xml
<build> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-war-plugin</artifactid> <version>3.0.0</version> </plugin> </plugins> </build>
Comments
Post a Comment