我无法使用简单的Spring示例,Spring版本4.1.7.RELEASE。
的web.xml:
<web-app id="epw" version="2.5"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<display-name>EPW</display-name>
<servlet>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Doesn't seem to help
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/epw-config.xml WEB-INF/SpringDispatcher-servlet.xml</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcher</servlet-name>
<url-pattern>/epw/*</url-pattern>
</servlet-mapping>
<!-- Doesn't seem to help
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/epw-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
-->
</web-app>
SpringDispatcher-servlet.xml中:
<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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="g.n.h.a.b.e"/>
<context:annotation-config/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/templates/"/>
<property name="suffix" value=".html"/>
</bean>
</beans>
DemoController.java:
package g.n.h.a.b.e;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value="/epw")
public class DemoController {
public DemoController() {
log.info("created");
}
@RequestMapping(value="/foo", method=RequestMethod.GET, produces="text/plain")
public String foo(@RequestParam(value="name", required=false, defaultValue="bar") final String name, Model model, final HttpServletRequest request) {
log.entry();
String view = "foo" ;
try {
model.addAttribute("name", name);
return view ;
} finally {
log.exit(view);
}
}
@RequestMapping(value="/riddle", method=RequestMethod.GET)
public ModelAndView doRiddle(Model model) {
log.entry();
String view = "foo";
ModelAndView mav = null ;
try {
model.addAttribute("name", "riddle");
mav = new ModelAndView(view, null, model) ;
return mav ;
} finally {
log.exit(mav) ;
}
}
private static final Logger log = LogManager.getLogger() ;
}
WAR内容:
C:\ projects \ bne_main \ JavaProjects \ webApps \ epw&gt; jar tf target \ epw.war
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
WEB-INF/classes/g/
WEB-INF/classes/g/n/
WEB-INF/classes/g/n/h/
WEB-INF/classes/g/n/h/a/
WEB-INF/classes/g/n/h/a/b/
WEB-INF/classes/g/n/h/a/b/e/
WEB-INF/classes/templates/
WEB-INF/lib/
index.html
WEB-INF/classes/g/n/h/a/b/e/DemoController.class
WEB-INF/classes/log4j.xml
WEB-INF/classes/log4j2.xml
WEB-INF/classes/revision.class
WEB-INF/classes/revision.properties
WEB-INF/classes/templates/foo.html
WEB-INF/epw-config.xml
WEB-INF/lib/aopalliance-1.0.jar
WEB-INF/lib/commons-logging-1.2.jar
WEB-INF/lib/groovy-all-2.4.3.jar
WEB-INF/lib/javassist-3.16.1-GA.jar
WEB-INF/lib/log4j-api-2.3.jar
WEB-INF/lib/log4j-core-2.3.jar
WEB-INF/lib/ognl-3.0.8.jar
WEB-INF/lib/slf4j-api-1.6.6.jar
WEB-INF/lib/spring-aop-4.1.7.RELEASE.jar
WEB-INF/lib/spring-beans-4.1.7.RELEASE.jar
WEB-INF/lib/spring-context-4.1.7.RELEASE.jar
WEB-INF/lib/spring-core-4.1.7.RELEASE.jar
WEB-INF/lib/spring-expression-4.1.7.RELEASE.jar
WEB-INF/lib/spring-jdbc-4.1.7.RELEASE.jar
WEB-INF/lib/spring-tx-4.1.7.RELEASE.jar
WEB-INF/lib/spring-web-4.1.7.RELEASE.jar
WEB-INF/lib/spring-webmvc-4.1.7.RELEASE.jar
WEB-INF/lib/thymeleaf-2.1.4.RELEASE.jar
WEB-INF/lib/unbescape-1.1.0.RELEASE.jar
WEB-INF/SpringDispatcher-servlet.xml
WEB-INF/web.xml
META-INF/maven/g.n.h.a.b/e/pom.xml
META-INF/maven/g.n.h.a.b/e/pom.properties
META-INF/INDEX.LIST
根据部署日志,/epw/riddle
和/epw/foo
都已映射:
21-Jul-2015 11:59:00.054 INFO [http-nio-8080-exec-94] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive C:\tomcat-8.0.24\webapps\epw.war
21-Jul-2015 11:59:01.568 INFO [http-nio-8080-exec-94] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
21-Jul-2015 11:59:01.677 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'SpringDispatcher': initialization started
21-Jul-2015 11:59:01.708 INFO [http-nio-8080-exec-94] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'SpringDispatcher-servlet': startup date [Tue Jul 21 11:59:01 EDT 2015]; root of context hierarchy
21-Jul-2015 11:59:01.755 INFO [http-nio-8080-exec-94] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/SpringDispatcher-servlet.xml]
21-Jul-2015 11:59:02.176 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod Mapped "{[/epw/riddle],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView gov.ny.health.adsg.bne.epw.DemoController.doRiddle(org.springframework.ui.Model)
21-Jul-2015 11:59:02.176 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod Mapped "{[/epw/foo],methods=[GET],produces=[text/plain]}" onto public java.lang.String gov.ny.health.adsg.bne.epw.DemoController.foo(java.lang.String,org.springframework.ui.Model,javax.servlet.http.HttpServletRequest)
21-Jul-2015 11:59:02.254 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'SpringDispatcher-servlet': startup date [Tue Jul 21 11:59:01 EDT 2015]; root of context hierarchy
21-Jul-2015 11:59:02.301 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: WebApplicationContext for namespace 'SpringDispatcher-servlet': startup date [Tue Jul 21 11:59:01 EDT 2015]; root of context hierarchy
21-Jul-2015 11:59:02.691 INFO [http-nio-8080-exec-94] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'SpringDispatcher': initialization completed in 1014 ms
21-Jul-2015 11:59:02.691 INFO [http-nio-8080-exec-94] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive C:\tomcat-8.0.24\webapps\epw.war has finished in 2,637 ms
在运行时,localhost:8080/epw/
按预期显示index.html
。但是,localhost:8080/epw/foo
和localhost:8080/epw/riddle
都会在日志中返回404而没有任何其他内容。
我错过了什么?
答案 0 :(得分:0)
您的调度程序servlet似乎映射到/ epw的上下文路径(web.xml):
<servlet-mapping>
<servlet-name>SpringDispatcher</servlet-name>
<url-pattern>/epw/*</url-pattern>
</servlet-mapping>
这意味着,您的应用应该在localhost:8080/epw/epw/foo
而非localhost:8080/epw/foo
下回复。您可以尝试更改servlet映射,或从控制器中删除@RequestMapping注释。
答案 1 :(得分:0)
确保您正在通过任何链接点击的网址,或通过jquery点击的任何点击操作,它会在控制器中附加上下文网址以及您的网址映射。就像我的情况一样,我通过jquery填充了一个数据表,并且在表的每一行上我绑定了一个重写的点击动作,让我们说url“/ projects”; 然后我重新确认我错过了我的上下文url,然后使用“/ context_url / projects”查看了映射。
答案 2 :(得分:0)
试试这个:
模型值可以直接在视图中使用,不需要通过ModelAndView构造函数传递它。
@RequestMapping(value="/riddle", method=RequestMethod.GET)
public ModelAndView doRiddle(Model model) {
log.entry();
String view = "foo";
ModelAndView mav = new ModelAndView(view);
try {
model.addAttribute("name", "riddle");
return mav ;
} finally {
log.exit(mav) ;
}
}