我正在尝试使用java配置运行一个简单的Spring MVC Web应用程序。但是当我在Tomcat上启动我的应用程序时(右键单击Project并在Server上运行),它无法按预期将我重定向到home.jsp。它给出的错误是请求的资源不可用。
以下是我从Spring in Action:Edition 4 book中复制的代码。
以下是我尝试/检查的内容: 1.Tomcat起来了。我可以看到tomcat欢迎页面。
2.双击tomcat,设置"服务器位置"选项,"使用Tomcat安装(控制tomcat安装)"在Eclips。
3.部署大会:
/ src< - > WEB-INF /类
/ WebContent< ---> /
4.这只是动态网络项目,没有专家。
5.我得到的错误: Error that I am getting
Dispatcher类:SpringWebInitializer.class
package config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
}
WebConfig.class
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("config")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
RootConfig.class
package config;
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;
@Configuration
@ComponentScan(basePackages={"config"},
excludeFilters={
@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
})
public class RootConfig {
}
HomeController.class
package config;
import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value = "/", method = GET)
public String home() {
return "home";
}
}
针对home.jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
</head>
<body>
<h1>Welcome to Spittr</h1>
</body>
</html>
另请查找下面的tomcat日志:
127.0.0.1 - - [10/Sep/2016:11:18:19 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:18:21 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
127.0.0.1 - - [10/Sep/2016:11:21:56 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:21:56 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:22:15 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:22:15 +0530] "GET /favicon.ico HTTP/1.1" 200 21630
127.0.0.1 - - [10/Sep/2016:11:24:37 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:24:38 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:25:13 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
127.0.0.1 - - [10/Sep/2016:11:26:48 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:26:48 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
127.0.0.1 - - [10/Sep/2016:11:28:45 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:28:45 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:29:10 +0530] "GET /SpringMVC2/WEB-INF/views/home.jsp HTTP/1.1" 404 992
127.0.0.1 - - [10/Sep/2016:11:30:25 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:30:25 +0530] "GET /SpringMVC2/WEB-INF/classes/config/SpringWebInitializer.java HTTP/1.1" 404 992
127.0.0.1 - - [10/Sep/2016:11:31:49 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:31:49 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:32:31 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:32:39 +0530] "GET /host-manager/html HTTP/1.1" 401 2100
0:0:0:0:0:0:0:1 - tomcat [10/Sep/2016:11:32:44 +0530] "GET /host-manager/html HTTP/1.1" 403 2857
127.0.0.1 - - [10/Sep/2016:11:33:54 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:33:54 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
127.0.0.1 - - [10/Sep/2016:11:39:42 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:39:42 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:42:22 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
127.0.0.1 - - [10/Sep/2016:11:43:33 +0530] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [10/Sep/2016:11:43:34 +0530] "GET /SpringMVC2/ HTTP/1.1" 404 1016
答案 0 :(得分:0)
您需要使用@Bean注释标记您的bean,这是一个配置类。
视图解析器应该用@Bean注释标记,这样它才能真正“开始”工作。
执行此操作后,您应该可以请求并查看您的home.jsp
答案 1 :(得分:0)
好的,所以终于找到了解决方案。如果有人面临同样的问题,请离开这里。 我已经使用构建路径添加了所有必需的JARS,但不确定为什么应用程序无法在运行时识别这些JARS。所以我手动将所有JARS复制到/ WEB-INF / lib文件夹。它开始工作了。