简体   繁体   中英

Deployment Spring app with Tomcat, context not matched

I have deployed a Spring MVC app into Tomcat 6, but I have a problem with the application context uri.

If I run into tomcat (from Intellij Idea), default settings, it works great, all controllers are matched (from context "/"). Example: http://localhost:8080/simgranjas/getAnimals

The problem is if I deploy the war file, then the url becomes localhost:8080/projectName/ (from Idea the same behaviour would be changing default context in Tomcat run configuration ("/") with project name ("/projectName")). The url should be this, because there are other projects running in the same Tomcat server.

After that the controllers don't match it. They still start in "/". So a call from code would be "

http://localhost:8080/simgranjas/getAnimals

instead of

http://localhost:8080/projectName/simgranjas/getAnimals

(where it really is) and I get a non found error.

@Controller
public class WebController {

@RequestMapping(value = "/simgranjas", method = RequestMethod.GET)
public String listUsers(ModelMap model) {
    return "principal";
}

@RequestMapping("/simgranjas/getAnimals")
public String getAnimals(ModelMap model) {
    return "granjaAnimales";
}

I've tried changing web.xml mvc-dispatcher but it doesn't work. There it is:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>Simulador Granjas</display-name>

<welcome-file-list>
    <welcome-file>/</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

remove the "/"

in "/simgranjas" from controller

"/simgranjas/getAnimals" becomes "simgranjas/getAnimals"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM