简体   繁体   English

与弹簧集成的嵌入式tomcat

[英]embedded tomcat integrated with spring

I'm using maven + spring + hibernate to build a xml-free file webapp and started at Minimal Tomcat 7 embedding example 我正在使用Maven + Spring + Hibernate构建无xml文件的webapp,并从Minimal Tomcat 7嵌入示例开始

application structure: 应用结构:

webapp
  |_src/main/java
  |  |_com.myapp.test
  |    |_Main.java
  |    |_HelloController.java
  |    |_MvcConfig.java
  |_src/main/resources
  |  |_hello.jsp
  |_src/test/java
  |_src/test/resources

HelloController.java HelloController.java

@Controller
@RequestMapping("/welcome")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
    model.addAttribute("message", "Spring 3 MVC Hello World");
    return "hello";
}

MvcConfig.java MvcConfig.java

@Configuration
@EnableWebMvc
public class MvcConfig {
    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

while trying to adding spring feature in Main.java 尝试在Main.java添加spring功能时

Tomcat tomcat = new Tomcat();
tomcat.setPort(9090);
File base = new File("");
System.out.println(base.getAbsolutePath());
Context rootCtx = tomcat.addContext("", base.getAbsolutePath());            
AnnotationConfigWebApplicationContext aactx = new AnnotationConfigWebApplicationContext();
aactx.scan("com.myapp");
aactx.register(MvcConfig.class);
DispatcherServlet dispatcher = new DispatcherServlet(ctx);
Tomcat.addServlet(rootCtx, "SpringMVC", dispatcher);
rootCtx.addServletMapping("/*", "SpringMVC");
tomcat.start();

then I got this error when go to localhost:9090/welcome.jsp to check out the embedded server 然后当我进入localhost:9090/welcome.jsp检出嵌入式服务器时收到此错误

Jun 04, 2013 4:34:39 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/hello.jsp] in DispatcherServlet with name 'SpringMVC'
Jun 04, 2013 5:00:38 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'SpringMVC'

why I got this error and how to solve it? 为什么我收到此错误以及如何解决?

Thanks in advance! 提前致谢!

I am pretty sure that the suffix you have set to "/" is relative "WEB-INF". 我很确定您设置为“ /”的后缀是相对的“ WEB-INF”。 Try creating webapp/src/main/webapp/WEB-INF/ and place hello.jsp there. 尝试创建webapp / src / main / webapp / WEB-INF /并将hello.jsp放在此处。

What if you change 如果您改变了怎么办

File base = new File("");

to

File base = new File("src/main/resources");

Before getting started, make the adjustment that @RobBarreca points out. 在开始之前,进行@RobBarreca指出的调整。 It should be aactx not ctx. 应该是aactx而不是ctx。

There are two options when doing Spring with embedded Tomcat (7.0): addContext and addWebapp. 使用嵌入式Tomcat(7.0)进行Spring时有两个选项:addContext和addWebapp。 The latter is really the recommended way for getting started whereas addContext is the more advanced, gotta have control route. 确实是推荐的入门方法,而addContext是更高级的方法,必须具有控制路线。 It is fine that you use addContext, but there is more configuration information that you have to specify. 您可以使用addContext,但必须指定更多配置信息。 And I will focus on addContext in this answer. 在此答案中,我将重点介绍addContext。

You are really only missing one piece above and that is what Servlet Class will be used for JSP files? 您确实只缺少上面的内容,那就是用于JSP文件的Servlet类? I would imagine that you probably have a file called welcome.jsp and it may well be in the correct place. 我可以想象您可能有一个名为welcome.jsp的文件,它很可能位于正确的位置。 However, your above code will say how to process your Controller correctly only. 但是,以上代码将仅说明如何正确处理Controller。

The code that you would need to add to handle JSP files is as follows: 您需要添加以处理JSP文件的代码如下:

Wrapper jspServlet = rootCtx.createWrapper();
jspServlet.setName("jsp");
jspServlet.setServletClass("org.apache.jasper.servlet.JspServlet");
jspServlet.addInitParameter("fork", "false");
jspServlet.addInitParameter("xpoweredBy", "false");
jspServlet.setLoadOnStartup(2);
rootCtx.addChild(jspServlet);
rootCtx.addServletMapping("*.jsp", "jsp");

Actually you do need a second thing and that is to alter you servlet mapping for SpringMVC. 实际上,您确实需要第二件事,那就是更改SpringMVC的servlet映射。 It should be 它应该是

rootCtx.addServletMapping("/", "SpringMVC");

If you did star, then it might try to handle jsp's for you, which it can't do even if your new JspServlet Servlet Class has been added. 如果您确实加注了星号,那么它可能会尝试为您处理jsp,即使添加了新的JspServlet Servlet类,它也无法做到。

Now this works, but one additional improvement you might want to make depending upon what content your actually serving is to specify a View Class in MvcConfig.java. 现在可以使用,但是您可能要根据实际提供的内容进行另一项改进,就是在MvcConfig.java中指定一个View类。 This, for example, could allow you to handle jstl content if you did: 例如,如果这样做,则可以允许您处理jstl内容:

resolver.setViewClass(JstlView.class);

but there are many other cases where setting a View Class is preferable. 但在许多其他情况下,最好设置View Class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM