Spring MVC Hello World问题

时间:2014-03-26 07:42:36

标签: java spring spring-mvc tomcat7

我尝试在Spring MVC上开始开发,只是一个小项目,但我可以在Tomcat 7上部署我的项目,但我无法访问。

当我尝试使用此网址进行访问时:

  xxxx:8080/test/index
  xxxx:8080/test/
  xxxx:8080/test/home.do
  xxxx:8080/test/welcome

我有一个错误页面HTTP 404。

我不明白为什么,因为我完全按照turorial来创建这个项目。

这是我的web.xml     

<web-app  version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>Spring CXF Servlet Mocks Integration Spring</display-name>

<servlet>
    <servlet-name>court</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>court</servlet-name>
    <url-pattern>*</url-pattern>
</servlet-mapping>

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这是我的mvc-dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.soltani.slim" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

这是我的控制器:     包com.soltani.slim;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class WelcomeController {

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String helloWorld() {
    return "welcome";
}
}

我创建了一个包含以下内容的jsp文件:     

消息:$ {message}

我把这个文件放在这个位置: /WEB-INF/jsp/welcome.jsp

错误日志是:

 No mapping found for HTTP request with URI [/test/] in DispatcherServlet with name 'Welcome'
mars 26, 2014 10:57:39 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/test/home.html] in DispatcherServlet with name 'Welcome'
mars 26, 2014 10:57:44 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/test/index] in DispatcherServlet with name 'Welcome'
mars 26, 2014 10:58:02 AM org.springframework.web.servlet.DispatcherServlet     noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/test/index] in    DispatcherServlet with name 'Welcome'

3 个答案:

答案 0 :(得分:0)

在web.xml中更改此行

            <url-pattern>*</url-pattern>
   to
           <url-pattern>/</url-pattern>

答案 1 :(得分:0)

确保在tomcat上部署的战争名称为“test”。另外,根据您提供给我们的代码,您应该只访问此网址:http://xxxx:8080/test/welcome

同时更改<url-pattern>*</url-pattern> to <url-pattern>/*</url-pattern>。请确保在/WEB-INF/jsp/下有一个welcome.jsp页面。

答案 2 :(得分:0)

我没有发现错误,我不明白为什么会犯这个错误。 无论如何,我刚刚用Spring工具套件创建了一个Web项目。尽管我的项目几乎完全相同,但我的项目编译,部署和运行良好:)

感谢您的帮助。

最诚挚的问候。

相关问题