java - annotation configuration - how to say spring configuration class location -
spring doesn't see request mapping controller. try use annotation configuration without xml.
project structure:
config.java
package com.dnn.web.config; //imports @configuration @componentscan("com.dnn.spring") @enablewebmvc public class config { @bean public urlbasedviewresolver setupviewresolver() { urlbasedviewresolver resolver = new urlbasedviewresolver(); resolver.setprefix("/web-inf/views/"); resolver.setsuffix(".jsp"); resolver.setviewclass(jstlview.class); return resolver; } }
webinitializer.java
package com.dnn.web.config; //imports public class webinitializer implements webapplicationinitializer { public void onstartup(servletcontext servletcontext) throws servletexception { annotationconfigwebapplicationcontext ctx = new annotationconfigwebapplicationcontext(); ctx.register(config.class); ctx.setservletcontext(servletcontext); dynamic servlet = servletcontext.addservlet("dispatcher", new dispatcherservlet(ctx)); servlet.addmapping("/"); servlet.setloadonstartup(1); } }
helloworldcontroller.java
package com.dnn.spring.controller; //imports @controller public class helloworldcontroller { @requestmapping("/hello") public string hello( @requestparam(value = "name", required = false, defaultvalue = "world") string name, model model) { model.addattribute("name", name); return "helloworld"; } }
when invoke application see following result:
localhost:8080/dnn-project/hello
returns same result.
i don't understand made mistake.
p.s. source code of example: source
i suppose spring doesn't see config
, webinitializer
classes don't understand why , how fix it.
p.s.
pom.xml
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.dnn.web</groupid> <artifactid>dnn-project</artifactid> <packaging>war</packaging> <version>1.0.0-snapshot</version> <name>dnn-project maven webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.0.1.release</spring.version> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring dependencies --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalname>dnn-project</finalname> </build> </project>
p.s.3
if send request url
http://localhost:8080/hello
then
мар 22, 2014 1:44:33 pm org.apache.catalina.core.standardwrappervalve invoke severe: servlet.service() servlet [dispatcher] in context path [] threw exception [handler processing failed; nested exception java.lang.noclassdeffounderror: javax/servlet/jsp/jstl/core/config] root cause java.lang.noclassdeffounderror: javax/servlet/jsp/jstl/core/config @ org.springframework.web.servlet.support.jstlutils.exposelocalizationcontext(jstlutils.java:101) @ org.springframework.web.servlet.view.jstlview.exposehelpers(jstlview.java:135) ...
there fine spring configuration. there part missing jstl dependency:
<dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency>
tomcat not include jstl has shipped application.
Comments
Post a Comment