java - Sping MVC 在路径中缺少应用程序上下文
问题描述
简而言之,我遇到的问题是,当我打开应用程序 http://localhost:8080/[Application-Context]/ 一切正常。但是当我按下链接“/otherpage”时,我得到一个 404 错误,并且 URL 是 http://localhost:8080/otherpage。如果我手动编写 http://localhost:8080/[Application-Context]/otherpage 然后我被重定向到正确的页面。
我将 Spring MVC 与 Tomcat-9 一起使用。它使用了一个将应用程序上下文设置为“/ [Application-Context]”的战争
我的控制器
@RequestMapping(value = "/")
@Controller
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printHallo(ModelMap model){
return "hello";
}
@RequestMapping("/otherpage")
public String printHallo(ModelMap model){
return "otherpage";
}
}
JSP 文件你好
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html charset=utf-8"%>
<html>
<%@ include file="common/header.jspf"%>
<body>
<form:form action="/otherpage">
<input type="submit" value = "otherpage">
</form:form>
</body>
</html>
Web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
小服务程序
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="knox.frontend"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="31556926"/>
<mvc:annotation-driven />
</beans>
我已经阅读了几篇文章。但一直无法弄清楚该怎么做。这似乎与Tomcat配置有关。
我找到了一些解决方案,但它们感觉不像是正确的做法。但我是 Spring 新手,所以我可能错了。
如果我链接到“otherpage”而不是“/otherpage”,那么它可以工作,但我仍然不能通过“/”来访问主页。
如果我将应用程序上下文更改为“/”,那么事情也可以正常工作,但这似乎是一个可能让我头疼的解决方案。因为感觉就像我只是在回避这个问题。
我还在一篇文章中读到,您应该在链接前添加 ${pageContext.servletContext.contextPath}。这也有效,但这只适用于 JSP 文件。看起来,就像一些东西,应该可以自动添加。
所以我的问题是,当涉及到客户端的链接时,处理应用程序上下文的正确方法是什么。
解决方案
一个简短的答案是 Spring MVC 的工作方式,您需要放入 jsp 文件:
<form:form action="HelloWeb/otherpage">
推荐阅读
- java - 如何使用 java 查找响应有效负载 (JSON) 大小
- knockout.js - Select2 Knockout 多选设置值问题
- python-3.x - 如何使用 tkinter StringVar 使大于或小于
- c# - 如何在运行时使 FlowLayoutPanel 可调整大小?
- python - 为什么我不能覆盖“filedialog.askdirectory”给定目录中的文件?
- dax - 测量平均值
- mongodb - 如何用另一个数组字段的平均值更新 MongoDB 中所有文档中的字段?
- python-3.x - 导入 pandas.io.common 时运行 vispr 会抛出 EmptyDataError
- c - 多平台 C BLE 依赖问题
- typescript - 打字稿制作一个以数组为键,以数组为值的地图