Spring MVC 请求映射中的浏览器 URL 错误

Browser URL error in Spring MVC request mapping

请不要拒绝这个问题,因为我已经经历了很多相关问题,但找不到我要解决的具体问题。请原谅 post 中的任何不足之处,因为我才刚刚开始回答 post 问题。我在 URL 提交表单或从一页导航到另一页导致错误 404 时遇到问题。我使用的是 glassfish server 4.1.1。我在 :

有一个包含 hyperlink/form 的欢迎页面
`http://localhost:8080/2_ReadingFormData`.

当我尝试通过手动输入 url 导航到另一个页面时,它起作用了:

`http://localhost:8080/2_ReadingFormData/showForm`

但是当我尝试使用 hyperlink/form 导航到页面时,URL 不包含我的项目目录,导致 404 错误:

`http://localhost:8080/showForm`

我找不到发生这种情况的原因。在某些表单提交的情况下,它似乎工作正常。在此特定示例中,表单确实有效,但由于上述原因,超链接无效。这种奇怪行为背后的原因是什么?

FormController.java

package spmvcform;

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

@Controller
@RequestMapping("/")
public class FormController {

    @RequestMapping //or @RequestMapping("/")
    public String showMainPage()
    {
        return "mainpage";
    }

    @RequestMapping("/showForm")
    public String showForm()
    {
        return "myform";
    }

    @RequestMapping("/processForm")
    public String processForm()
    {
        return "helloworld";
    }

}

mainpage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head> 
    <body>
        <h1>This is the main page</h1>
        <a href="showForm">Next</a>
    </body>
</html>

myform.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Form Page</h1><br/>
        <form action="processForm" method="get">
            <input type="text" name="uname"
                   placeholder="what's your name?"/>

            <input type="submit" name="Next"/>

        </form>
    </body>
</html>

helloworld.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
        <h1>Student name : ${param.uname}</h1>
    </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

</web-app>

调度员-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc 
                           http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:mvc="http://www.springframework.org/schema/mvc">


    <!-- Add support for component scanning-->
    <context:component-scan base-package="spmvcform"/>

    <!-- Add support for conversion, formatting and validation support-->   
    <mvc:annotation-driven/>

</beans>

我在 Glassfish 服务器 4.1.1 中使用 netbeans 8.2。

发生这种情况是因为您提供的是相对路径而不是绝对路径 path.so 在 showForm 之前添加斜线,例如 <a href="/showForm">Next</a> 应该可以正常工作。

您可以像下面这样添加上下文路径。

<a href="${pageContext.request.contextPath}/showForm">Next</a>

将上下文路径放在一个变量中,并在整个页面中使用它,如下所示。

// somewhere on the top of your JSP
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

...
<a href="${contextPath}/showForm">Next</a>

difference-between-relative-path-and-absolute-path-