Java – spring: URL does not parse the link correctly

Although I am experienced in Java, I am unfamiliar with the spring framework and web applications When I run my site on the local Tomcat server, the URL is: http: / / localhost: 8080 / myapp/

Now request mapping to delegate me to my home page:

@RequestMapping(value = "/",method = RequestMethod.GET)
public String someMethod(Model model) { ... 
   return "index"; }

Now in index In the XHTML file, I use < a href = "apps /" > link < / a > to link to another page, but when I want to link back to the index page, I must use < a href = ".. / index /" > link < / a > I searched for a solution and found:

<spring:url value='/apps' var="apps_url" />
<a href="${apps_url}">link</a>

But spring: URL always resolves to http: / / localhost: 8080 / myapp / – my current page In addition, when I only use such a link: < a href = "/ othersite" > link < / a >, it always resolves to http: / / localhost: 8080 / othersite instead of http: / / localhost: 8080 / myapp / othersite, as I expected How do I make my links work? Is http: / / localhost: 8080 / myapp implicitly defined as my context or can / should it be changed to http: / / localhost: 8080 /?

In addition, is there any connection between the URL on the local Tomcat server and the URL when the web application is published?

Here are some of my application files:

Servlet's context In XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <context:component-scan base-package="myApp" />

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- Handles HTTP GET requests for /resources/** and /css/** by efficiently 
    serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    <resources mapping="/css/**" location="/css/" />

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".xhtml" />
    </beans:bean>

</beans:beans>

From web xml:

<!-- The deFinition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Solution

It's best to put all the links below

<a href="${pageContext.servletContext.contextPath}/othersite"> Other site </a>

${pagecontext. ServletContext. Contextpath} always provides root permission for your application. When you develop and use http: / / localhost: 8080 / myapp, your application root is / myapp, but when you want to put the application in the production environment, your application root will usually be /, Use ${pagecontext. ServletContext. Contextpath} before linking to make sure it works in both cases

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>