【问题标题】:Simple Request mapping not working, Beginners in Spring简单的请求映射不起作用,Spring 初学者
【发布时间】:2013-04-15 06:26:48
【问题描述】:

我是 spring mvc 的初学者,我有 2 个 jsp:- 1. Webcontent/index.jsp:这很好用。索引文件有一个超链接文本,如:-

      <a href="hello.html" rel="nofollow">Say Hello</a>
  1. WebContent/WEB-INF/jsp/hello.jsp : 在正文中显示以下内容

         ${message}
    

项目容器如下:-

 @Controller
  public class HelloWorldContainer {

 @RequestMapping(value="/hello", method=RequestMethod.GET)
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello.jsp", "message", message);
    }
}

以下是 WebContent/WEB-INF/web.xml 文件:-

   <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.*</url-pattern>
</servlet-mapping>

WebContent/WEB-INF/spring-servlet.xml :-

 <context:component-scan
    base-package="org.explorear.ar" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

我的问题:- 当我从eclipse在tomcat服务器上运行这个项目时,会显示索引文件 完全没问题。但是由于索引文件中的文本超链接到 hello.html 我不断得到 Http 状态 404。

【问题讨论】:

  • 请打开调试级别的日志记录,向我们展示 404 的确切来源。没有日志,一切都只是猜测 :)
  • 请检查服务器是否启动正常。

标签: java spring jakarta-ee spring-mvc tomcat7


【解决方案1】:

这就是你的 web.xml 应该的样子。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

  <display-name>Spring3MVC</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml
    </param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <session-config>  
  <session-timeout>10</session-timeout>  
</session-config> 
</web-app>

【讨论】:

    【解决方案2】:

    我可能会遗漏一些东西,但我认为

    org.springframework.web.context.ContextLoaderListener

    并且需要在 web.xml 中提供 context-param

    【讨论】:

      【解决方案3】:

      您将在此行返回整个页面名称

      return new ModelAndView("hello.jsp", "message", message);

      并且您正在使用 ViewResolver ,其中您使用 .jsp 为返回值添加后缀。所以在我的建议中使用这个

      return new ModelAndView("hello", "message", message);

      【讨论】:

      • 完成了...我正在导航到 localhost:8080/Spring3MVC/hello.html 但仍然是 http 状态 404
      • 您正在使用 hello.jsp 但在您的参考中您使用的是 hello.html?您正在使用 jsp 页面并在参考链接中调用 html 页面?将其更改为 hello.jsp
      • 使用这个作为servlet映射&lt;servlet-mapping&gt; &lt;servlet-name&gt;spring&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt;
      • 在你的spring-servlet.xml中使用这个&lt;context:component-scan base-package="your base package" /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix"&gt; &lt;value&gt;/WEB-INF/jsp/&lt;/value&gt; &lt;/property&gt; &lt;property name="suffix"&gt; &lt;value&gt;.jsp&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt;把你所有的jsp文件放在/WEB-INF/jsp/文件夹中
      • 再次没有运气。我用过这个教程viralpatel.net/blogs/…
      猜你喜欢
      • 2014-05-15
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2017-03-05
      • 2014-04-18
      相关资源
      最近更新 更多