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

时间:2018-11-26 06:01:27

标签: spring-mvc glassfish request-mapping xml-configuration

请不要拒绝这个问题,因为我经历了许多相关问题,也找不到我要解决的特定问题。因为我刚开始发布问题,所以请谅解我在发布中的任何缺点。我遇到URL提交表单或从一页导航到另一页的问题,导致错误404。我正在使用glassfish服务器4.1.1。我有一个欢迎页面,其中包含超链接/表单:

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

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

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

但是当我尝试使用超链接/表单导航到页面时,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>

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

我正在将Netbeans 8.2与Glassfish服务器4.1.1一起使用。

1 个答案:

答案 0 :(得分:1)

之所以会这样,是因为您提供的是相对路径而不是绝对路径。因此在showForm之前添加斜杠(如<a href="/showForm">Next</a>应该可以正常工作。

OR

您可以添加如下所示的上下文路径。

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

OR

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

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