SpringMVC 无法到达控制器,每次都抛出 404

我的项目结构如下所示 -

http://img4.mukewang.com/6288dc120001058103300544.jpg

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>


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


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


白猪掌柜的
浏览 122回答 3
3回答

慕勒3428872

尝试以下操作:更改@RequestMapping(value="/login.htm")为&nbsp;@RequestMapping(value="/login")更改<a href="login.htm">Login</a>为&nbsp;<a href="/SpringWebApp/login">Login</a>看起来您提供的链接不正确,因此您的控制器没有接收到它。

子衿沉夜

在你的web.xml你还没有定义spring-servlet.xml. 所以像这样定义spring-servlet.xml。web.xml<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp;xmlns="http://xmlns.jcp.org/xml/ns/javaee"&nbsp;xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"&nbsp;id="WebApp_ID" version="4.0">&nbsp; <display-name>SpringWebApp</display-name>&nbsp; &nbsp; <servlet>&nbsp; &nbsp; <servlet-name>spring</servlet-name>&nbsp; &nbsp; <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>&nbsp; &nbsp; <load-on-startup>1</load-on-startup>&nbsp; </servlet>&nbsp; <servlet-mapping>&nbsp; &nbsp; <servlet-name>spring</servlet-name>&nbsp; &nbsp; <url-pattern>/</url-pattern>&nbsp; </servlet-mapping>&nbsp; &nbsp;<context-param>&nbsp; &nbsp; <param-name>contextConfigLocation</param-name>&nbsp; &nbsp; <param-value>WEB-INF/spring-servlet.xml</param-value>&nbsp; &nbsp;</context-param>&nbsp; <welcome-file-list>&nbsp; &nbsp; <welcome-file>index.html</welcome-file>&nbsp; &nbsp; <welcome-file>index.htm</welcome-file>&nbsp; &nbsp; <welcome-file>index.jsp</welcome-file>&nbsp; &nbsp; <welcome-file>default.html</welcome-file>&nbsp; &nbsp; <welcome-file>default.htm</welcome-file>&nbsp; &nbsp; <welcome-file>default.jsp</welcome-file>&nbsp; </welcome-file-list></web-app>现在写下你的spring-servlet.xml喜欢<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"&nbsp; &nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; xmlns:p="http://www.springframework.org/schema/p"&nbsp; &nbsp; xmlns:context="http://www.springframework.org/schema/context"&nbsp; &nbsp; xsi:schemaLocation="http://www.springframework.org/schema/beans&nbsp; &nbsp; http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&nbsp; &nbsp; http://www.springframework.org/schema/context&nbsp; &nbsp; http://www.springframework.org/schema/context/spring-context-3.0.xsd">&nbsp; &nbsp; <context:component-scan base-package="pac.test" />&nbsp; &nbsp; <mvc:annotation-driven/>&nbsp; &nbsp; <bean&nbsp; &nbsp; &nbsp; &nbsp; class="org.springframework.web.servlet.view.InternalResourceViewResolver">&nbsp; &nbsp; &nbsp; &nbsp; <property name="prefix" value="/WEB-INF/views/" />&nbsp; &nbsp; &nbsp; &nbsp; <property name="suffix" value=".jsp" />&nbsp; &nbsp; </bean></beans>像这样写你的控制器@Controllerpublic class LoginController {&nbsp; &nbsp; @RequestMapping(value="/")public ModelAndView loginRequest() {&nbsp; &nbsp; &nbsp; &nbsp; ModelAndView mv = new ModelAndView("home");&nbsp; &nbsp; &nbsp; &nbsp; return mv;&nbsp; &nbsp; }}而在index.jsp页面中,而不是 <a href="login.htm">Login</a>像这样写重写 <a href="/SpringWebApp/">Login</a>

天涯尽头无女友

@RequestMapping(value="/login.htm")&nbsp; &nbsp; &nbsp; &nbsp;public ModelAndView loginRequest() {&nbsp; &nbsp; ModelAndView mv = new ModelAndView("home");&nbsp; &nbsp; return mv;}像这样从“/login.htm”中删除“.htm”&nbsp;@RequestMapping(value="/login")&nbsp; &nbsp; &nbsp; &nbsp;public ModelAndView loginRequest() {&nbsp; &nbsp; ModelAndView mv = new ModelAndView("home");&nbsp; &nbsp; return mv;}并像这样更正您的超链接<h2>&nbsp; &nbsp; <a href="/login">Login</a></h2>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java