Spring MVC 路径中缺少应用程序上下文

huangapple go评论67阅读模式
英文:

Sping MVC Missing Application Context in Path

问题

简而言之,我遇到的问题是,当我打开应用程序 http://localhost:8080/[Application-Context]/ 时,一切正常。但是,当我点击链接 "/otherpage" 时,我会收到 404 错误,并且 URL 是 http://localhost:8080/otherpage。如果我手动输入 http://localhost:8080/[Application-Context]/otherpage ,那么我会被重定向到正确的页面。

我正在使用 Spring MVC 以及 Tomcat-9。它使用一个 exploded war 文件,应用程序上下文设置为 "/[Application-Context]"。

我的控制器:

@RequestMapping(value = "/")
@Controller
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String printHello(ModelMap model){
        return "hello";
    }
    @RequestMapping("/otherpage")
    public String printOtherPage(ModelMap model){
        return "otherpage";
    }
}

JSP 文件 hello:

<%@ 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>

Servlet 配置:

<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 文件中起作用。而且,这似乎是可以自动添加的东西。

因此,我的问题是,处理客户端链接时,关于应用程序上下文的正确方法是什么?

英文:

In short the problem I am having is that when, I open the application http://localhost:8080/[Application-Context]/ everything works. But then when I press a link "/otherpage", i get a 404 error and the URL is http://localhost:8080/otherpage. And if I manually write http://localhost:8080/[Application-Context]/otherpage then I'm redirected to the correct page.

I'm using Spring MVC together with Tomcat-9.
Which uses a war exploded with the application context set to "/[Application-Context]"

My Controller

@RequestMapping(value = &quot;/&quot;)
@Controller
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String printHallo(ModelMap model){
        return &quot;hello&quot;;
    }
    @RequestMapping(&quot;/otherpage&quot;)
    public String printHallo(ModelMap model){
        return &quot;otherpage&quot;;
    }
}

The JSP file hello

&lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot; %&gt;
&lt;%@ page contentType=&quot;text/html charset=utf-8&quot;%&gt;
    &lt;html&gt;
        &lt;%@ include file=&quot;common/header.jspf&quot;%&gt;
        &lt;body&gt;

            &lt;form:form action=&quot;/otherpage&quot;&gt;
                &lt;input type=&quot;submit&quot; value = &quot;otherpage&quot;&gt;
            &lt;/form:form&gt;

        &lt;/body&gt;
    &lt;/html&gt;

The Web.xml

&lt;web-app xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee
                        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version=&quot;3.0&quot;&gt;
    &lt;display-name&gt;Spring MVC Application&lt;/display-name&gt;

    &lt;servlet&gt;
        &lt;servlet-name&gt;HelloWeb&lt;/servlet-name&gt;
        &lt;servlet-class&gt;
            org.springframework.web.servlet.DispatcherServlet
        &lt;/servlet-class&gt;

        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;HelloWeb&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;

&lt;/web-app&gt;

The Servlet

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
       xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xsi:schemaLocation=&quot;
            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">
    &lt;context:component-scan base-package=&quot;knox.frontend&quot;/&gt;
    &lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
        &lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/view/&quot;/&gt;
        &lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot;/&gt;
    &lt;/bean&gt;

    &lt;mvc:resources mapping=&quot;/resources/**&quot; location=&quot;/resources/&quot;
                   cache-period=&quot;31556926&quot;/&gt;

    &lt;mvc:annotation-driven /&gt;

&lt;/beans&gt;

I have read several posts. but have not been able to figure out what to do. It seems to be something to do with the Tomcat configuration.

I have found some solutions, but they don't feel like the correct way of doing. But I am new to Spring so I might be wrong.

If I link to "otherpage" instead of "/otherpage", then it works, but i still can't yes "/" to get to the homepage.

If I change the application context to just "/", then things works as well, but that seems like a solution which might give me a headache down the line. As it feels like I am just avoiding the issue.

I also read in a post, that you should add ${pageContext.servletContext.contextPath} in front of links. Which also worked, but that would only work in the JSP files. And it seems, like something, which should be possible to add automatically.

So my question is, what is the correct way of handling application context, when it comes to links on the client side.

答案1

得分: 1

一个简短的回答是这样的,这是 Spring MVC 的工作原理,您需要将以下内容放入 JSP 文件中:

<form:form action="HelloWeb/otherpage">
英文:

A short answer would be that is how Spring MVC Works, you need to put in the jsp file:

        &lt;form:form action=&quot;HelloWeb/otherpage&quot;&gt;

huangapple
  • 本文由 发表于 2020年10月22日 21:45:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64483611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定