尝试升级到Spring4没有xml配置webapp

时间:2018-07-20 09:00:54

标签: java spring-mvc annotations spring-4

我正在尝试使用Spring 4构建一个小的Hello World WebApp原型,而没有任何xml文件配置。

比起我用tomcat 7,这是我的pom:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>

比我有这个配置类:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "it.spring4.configuration")
public class HelloWorldConfiguration {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new 
                 InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

}

和网络初始化程序类:

public class HelloWorldInitializer extends 
AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { HelloWorldConfiguration.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return null;
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

}

控制器:

@Controller
@RequestMapping("/")
public class HelloWorldController {

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello(ModelMap model) {
    model.addAttribute("greeting", "Hello World from Spring 4 MVC");
    return "welcome";
}

@RequestMapping(value = "/helloagain", method = RequestMethod.GET)
public String sayHelloAgain(ModelMap model) {
    model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");
    return "welcome";
}

}

并在WEB-INF下包含welcome.jsp的文件夹视图:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>HelloWorld page</title>
 </head>
 <body>
   Greeting : ${greeting}
 </body>
</html>

这是在here下找到的示例,无法正常工作。我尝试了其他类似的示例,但没有人工作..您能解释一下我错了吗?


抱歉,错误是:

HTTP Status 404 – Not Found
Type Status Report

Message /Spring4FullAnnotation/

Description The origin server did not find a current representation for the 
target resource or is not willing to disclose that one exists.

该死的。我不下载示例,我只是在日食中复制代码...我没有maven项目,我仅使用maven来解决依赖关系(我在之后创建了一个动态Web项目配置->转换为maven项目),通常我会在服务器上运行它。我已经用Spring4建立了许多如上所述的项目,它们可以正常运行。

我不知道为什么我不能启动它。代码很简单,并且功能不能依赖于Maven配置。

谢谢

1 个答案:

答案 0 :(得分:0)

代码对于以下URL正常工作

http://localhost:8080/Spring4MVCHelloWorldNoXMLDemo-1.0.0/

请检查您的URL