Tomcat中的Spring处理函数映射
我无法获得一个简单的Spring例子,Spring版本4.1.7.RELEASE。Tomcat中的Spring处理函数映射
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:\项目\ bne_main \ JavaProjects \ WEBAPPS \ EPW>罐子TF目标\ 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,而且日志中没有任何更多内容。
我错过了什么?
回答:
看来你的分发程序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注释。
回答:
请确保您通过任何链接点击的网址,或通过jquery进行的任何点击操作,它会在控制器中追加上下文url以及您的url映射。就像我的情况一样,我通过jquery填充数据表,并在表的每一行上绑定了一个点击动作,该动作正在重新渲染以让URL“/ projects”; 然后我重新认识到我错过了我的上下文url,然后用“/ context_url/projects”改变了映射。
回答:
尝试这种情况:
模型值可以在视图可以直接使用,将其通过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) ;
}
}
以上是 Tomcat中的Spring处理函数映射 的全部内容, 来源链接: utcz.com/qa/261252.html