【问题标题】:SpringMVC - URL resource couldnt be foundSpring MVC - 找不到 URL 资源
【发布时间】:2019-12-02 21:48:15
【问题描述】:

我是一名从事 Spring MVC 框架的学生。如果这是一个不好的问题,请提前道歉

我正在使用 maven 部署到 tomcat7 我了解 springMVC 的工作流程,我将尝试解释它。请纠正我的任何误解

  1. 客户端请求会先命中web.xml

  2. 1234563如果我希望它是另一个名字)
  3. 在 [servletName]-servlet.xml 中,它将前缀和后缀连接到 URL,使其成为文件路径(例如 /WEB-INF/jsp/ + hello + .jsp)来定位正确的 jspFile 以在客户端浏览器中呈现

  4. 控制器文件(声明为@Controller)充当后端并使用@RequestMapping(URL) 调用

基于此流程,我输入“localhost:8080/hello.jsp”(也尝试过下面提到的其他 URL)但它仍然返回 HTTP404

tomcat7 运行良好,只是找不到jsp页面

我尝试将 web.xml 中的“url-patterns”编辑为“/”或“.jsp”或“hello.jsp”或“hello”,但无济于事

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>to do list</display-name>

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

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

    <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

HelloWeb-servlet.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:context = "http://www.springframework.org/schema/context"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   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.tutorialspoint" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>

你好.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
   <head>
      <title>Hello Spring MVC</title>
   </head>

   <body>
      <h2>${message}</h2>
   </body>
</html>

HelloController.java

@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
       public String printHello(ModelMap model) {
          model.addAttribute("message", "Hello Spring MVC Framework!");
          return "hello";
       }
}

这是我的文件层次结构

https://i.stack.imgur.com/msr7k.png

这是错误

https://i.stack.imgur.com/hVUsv.png

【问题讨论】:

  • 您是否需要使用外部 Tomcat 容器,例如讲师,还是您可以选择如何部署?
  • 我使用 maven 使用命令“tomcat7:run”部署它
  • 你能决定不使用Tomcat,还是别人告诉你使用Tomcat?
  • tomcat 是 SpringMVC 框架的流行选择,并被大多数教程使用。 tomcat7 是否存在已知问题,您不鼓励我使用它?
  • 但是如果我能够继续到这一点i.stack.imgur.com/hVUsv.png 它表明tomcat7 工作正常,对吧?只是找不到指定的任何资源

标签: java maven spring-mvc tomcat7-maven-plugin


【解决方案1】:

尝试将此代码配置用于 servlet。您尝试访问http://localhost:8080/hello 吗?

       <servlet>
          <servlet-name>HelloWeb</servlet-name>
          <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
          </servlet-class>
          <init-param>
                <param-name>contextConfigLocation</param-name>
               <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
            </init-param>
          <load-on-startup>1</load-on-startup>
       </servlet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多