spring-mvc - Spring MVC 请求映射中的浏览器 URL 错误
问题描述
请不要拒绝这个问题,因为我已经经历了许多相关的问题并且找不到我要解决的具体问题。请原谅我在帖子中的任何缺点,因为我才开始发布问题。我遇到了 URL 提交表单或从一个页面导航到另一个页面导致错误 404 的问题。我使用的是 glassfish 服务器 4.1.1。我有一个包含超链接/表单的欢迎页面:
`http://localhost:8080/2_ReadingFormData`.
当我尝试通过手动输入网址导航到另一个页面时,它可以工作:
`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";
}
}
主页.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>
我的表单.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>
应用程序上下文.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。
解决方案
发生这种情况是因为您提供的是相对路径而不是绝对路径。所以在 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>
推荐阅读
- java - 在 google 磁盘 api 中使用凭据的问题
- c# - 3D 空间中的激光投影仪校准
- git - Jenkins Git Changelog 插件在与 JIRA 交谈时抛出“无协议”错误
- algorithm - 对矩阵按行和按列进行排序
- java - Java 扫描器限制
- vue.js - Vue CLI 3 不会将供应商转换为 ES5
- php - Sofa/eloquence 在 null 上调用成员函数 make()
- java - 设备主页中的 Android 屏幕
- visual-studio - 禁用鼠标悬停时弹出的 Visual Studio 快速信息
- c# - 无法在 .net 标准 2.0 的 VS 2017 15.8.5 上运行/发布天蓝色函数