0

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

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

  1. 客户端请求将首先访问 web.xml

  2. 在 web.xml 中,URL 模式与一个 servletName(或 DispatcherServlet)相关联,它会将其映射到(默认情况下)另一个名为“[servletName]-servlet.xml”的 xml(如果我想要它,我也可以实例化它)是另一个名字)

  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

4

1 回答 1

0

尝试将此代码配置用于 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>
于 2019-07-24T19:34:14.233 回答