简体   繁体   English

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

[英]trying to upgrade to Spring4 no xml configuration webapp

I'm trying to build a little hello world WebApp prototype with Spring 4 without any xml file configuration. 我正在尝试使用Spring 4构建一个小的Hello World WebApp原型,而没有任何xml文件配置。

Than I use tomcat 7 and here is my pom: 比起我用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>

Than I have this configuration class: 比我有这个配置类:

@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;
    }

}

and the web initializer class: 和网络初始化程序类:

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[] { "/" };
}

}

The controller: 控制器:

@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";
}

}

and under WEB-INF the folder views that contains welcome.jsp: 在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>

This is an example found here and doesn't work. 这是一个示例,在这里不起作用。 I tried other similar examples but noone works.. can you explain me were I'm wrong? 我尝试了其他类似的示例,但是没有人工作..您能解释一下我错了吗?


Sorry the error is: 对不起,错误是:

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.

Damn.. I don't dowload the example I just copy the code in my eclipse... I don't have a maven project, I use maven just for resolving the dependencies (I create a Dynamic Web Project after that Configure -> Convert to maven project), than I normally run it on server. 该死的..我不下载示例,我只是在eclipse中复制代码...我没有maven项目,我仅使用maven来解决依赖关系(我在配置->之后创建了一个动态Web项目。转换为Maven项目),比我通常在服务器上运行它。 I have many project built as described above with Spring4 that are perfectly working. 我有许多如上所述的Spring4构建的项目,它们运行良好。

I don't know why I can't launch this. 我不知道为什么我不能启动这个。 The code is simple and the functioning can't depends of maven configuration.. 代码很简单,并且功能不能依赖于Maven配置。

Thank you 谢谢

The code is working fine for the following URL 下列网址的代码运行正常

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

Please check your URL 请检查您的网址

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

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