【问题标题】:SpringMVC won't reach controller, throws 404 everytimeSpringMVC 无法到达控制器,每次都抛出 404
【发布时间】:2019-01-24 07:20:16
【问题描述】:

我的项目结构是这样的 -

web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 
id="WebApp_ID" version="4.0">
  <display-name>SpringWebApp</display-name>

    <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>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

spring-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="pac.test.*" />

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

控制器 -

package pac.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {

    @RequestMapping(value="/login.htm")
public ModelAndView loginRequest() {

        ModelAndView mv = new ModelAndView("home");
        return mv;
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h2>
        <a href="login.htm">Login</a>
    </h2>
</body>
</html>

但是,在加载索引后,当 home.jsp 明显存在于路径中时,单击超链接会导致 404(找不到资源)。我根本无法理解我在这里做错了什么。我已经仔细检查了所有的咒语等和路径。它应该工作,但没有!

Spring 甚至没有以某种方式到达 Controller 类。

【问题讨论】:

  • 您能否从(value="/login.htm") 中删除/,然后再次检查是否有效。
  • @PrashantZombade 试过了。不起作用。相同的错误页面
  • loginRequest() 中添加一个sysout 以检查它是否正在访问此控制器/方法。否则无法找到 login.htm 的映射。
  • 它没有击中控制器。我无法弄清楚为什么会这样。
  • @RahulDev 你没有pom.xml吗?

标签: java spring spring-mvc jakarta-ee


【解决方案1】:

尝试以下方法:

  1. @RequestMapping(value="/login.htm") 更改为 @RequestMapping(value="/login")
  2. &lt;a href="login.htm"&gt;Login&lt;/a&gt; 更改为 &lt;a href="/SpringWebApp/login"&gt;Login&lt;/a&gt;

看起来您提供的链接不正确,因此您的控制器没有接收到它。

【讨论】:

    【解决方案2】:
    @RequestMapping(value="/login.htm")
           public ModelAndView loginRequest() {
        ModelAndView mv = new ModelAndView("home");
        return mv;
    }
    

    像这样从“/login.htm”中删除“.htm”

     @RequestMapping(value="/login")
           public ModelAndView loginRequest() {
        ModelAndView mv = new ModelAndView("home");
        return mv;
    }
    

    并像这样更正您的超链接

    <h2>
        <a href="/login">Login</a>
    </h2>
    

    【讨论】:

      【解决方案3】:

      在您的web.xml 中,您尚未定义spring-servlet.xml。所以像这样在web.xml 中定义spring-servlet.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 
      id="WebApp_ID" version="4.0">
        <display-name>SpringWebApp</display-name>
      
          <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>
      
         <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>WEB-INF/spring-servlet.xml</param-value>
         </context-param>
      
        <welcome-file-list>
          <welcome-file>index.html</welcome-file>
          <welcome-file>index.htm</welcome-file>
          <welcome-file>index.jsp</welcome-file>
          <welcome-file>default.html</welcome-file>
          <welcome-file>default.htm</welcome-file>
          <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
      </web-app>
      

      现在写你的spring-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="pac.test" />
          <mvc:annotation-driven/>
      
          <bean
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/views/" />
              <property name="suffix" value=".jsp" />
          </bean>
      </beans>
      

      像这样写你的控制器

      @Controller
      public class LoginController {
      
          @RequestMapping(value="/")
      public ModelAndView loginRequest() {
      
              ModelAndView mv = new ModelAndView("home");
              return mv;
          }
      }
      

      并且在index.jsp页面而不是写&lt;a href="login.htm"&gt;Login&lt;/a&gt;重写像&lt;a href="/SpringWebApp/"&gt;Login&lt;/a&gt;

      【讨论】:

        猜你喜欢
        • 2012-05-17
        • 1970-01-01
        • 2015-06-26
        • 2013-12-14
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 2014-06-19
        • 2021-10-12
        相关资源
        最近更新 更多