Basavaraj
Basavaraj

Reputation: 1111

Root Context path in spring application

I am running application using tomcat server, when server start i will get url

http://localhost:8080/TestApp/

and displaying index.jsp file but when i click link in index file it is displaying url like

http://localhost:8080/testsuccess

but it should display like

http://localhost:8080/TestApp/testsuccess

can any please help me to solve this.

SpringConfiguration.java

@Configuration
@EnableWebMvc
@ComponentScan("com.testapp")
@EnableTransactionManagement
public class SpringConfiguration {
   @Bean
   public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}
}

SpringWebAppInitializer.java

public class SpringWebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(SpringConfiguration.class);
    ServletRegistration.Dynamic dispatcher = container.addServlet(
            "SpringDispatcher", new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
}

}

MyController.java

@Controller
public class MyFirstController 
 {

@RequestMapping(value = "/" , method = RequestMethod.GET)
public String testApp() throws Exception{
    return "index";
}

@RequestMapping(value = "/testsuccess", method = RequestMethod.GET)
public String testAppSuccess() {
    Map<String, Object> model = new HashMap<String, Object>();
    return "success";
}

}

Upvotes: 0

Views: 3933

Answers (2)

meistermeier
meistermeier

Reputation: 8262

I think your problem comes from the link inside your index.jsp. They might look like <a href="/testsuccess">...</a> You should use either jstl or spring tag lib to handle links / urls in your pages. They both have the ability to prepend the deployment / context path of your application.

jstl example: with taglib xmlns:c="http://java.sun.com/jsp/jstl/core" included you can create an anchor like <a href="<c:url value='/testsuccess'/>">..</a>

spring example: with taglib xmlns:spring="http://www.springframework.org/tags" your link will be created in two steps:

<spring:url value="/testsuccess" var="myurl" htmlEscape="true"/>
<a href="${myurl}">...</a>

Update: Wrong function name in jstl taglib version.

Upvotes: 2

Basavaraj
Basavaraj

Reputation: 1111

got it i should use

 <a href="<c:url value='/testsuccess'/>">Next</a> 

it will give the context path.

Upvotes: 3

Related Questions