3
votes

I'm trying to write a method which takes a @PathVariable parameter and redirects user to a jsp file.

@Controller
public class MainController 
{

    @RequestMapping("/user/{customerId}")
    // http://localhost:8080/Test/user/5
    public String getCustomerById(@PathVariable("customerId") String customerId, Model model) 
    {
            model.addAttribute("customer_id", customerId);
            // this is the user_details.jsp file, I need to show this jsp file to visitor
            return "user_details";
    }
}

When I try to navigate http://localhost:8080/SpringBlog/user/5 It's showing me an empty response. (Nothing Even In Page Source)

When I looked into Spring output console, it's showing me the following message when I'm trying to navigate :

2017-07-19 13:24:56.191 ERROR 6772 --- [io-8080-exec-75]

o.s.boot.web.support.ErrorPageFilter

Cannot forward to error page for request [/user/5] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

I've already tried following parameter descriptions as the followings :

@PathVariable(value="customerId") String customerId

@PathVariable(name="customerId") String customerId

@PathVariable("customerId") String customerId

@PathVariable String customerId

None of them worked, always empty response with same error message.

I'm sure that all files are in correct place, in my MainController Class I have several methods with No Parameters, RequestParams, etc.. all of them working as expected. But if I want to create a RequestMapping with @PathVariable, it always returns empty response and same error message in the output console.

But if I try same approach with @RestController it's working as expected:

@RestController
public class RestApi
{
    // http://localhost:8080/Test/api/user/56
    // Is Working, Returns Hello 56 As Response
    @RequestMapping("api/user/{customerId}")
    public String apiTest(@PathVariable("customerId") String customerId)
    {
        return "Hello "+customerId; 
    }
}

What am I missing ?

Application Details :

<packaging>war</packaging>
...
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
</parent>
...

Apache Tomcat/7.0.56
JVM 1.8.0_131-b11

Thanks for your help.

3
Are you sure that the name you are going to return is correct, I mean "user_details"? Did you defined ".jsp" postfix in application.properties?Afridi
Hi, I added some extra details about application. Yes, I have user_details.jsp in the folder with other jsp files, Every method works normally which does not have a parameter as @PathVariableEmir Civas

3 Answers

2
votes

The annotation @RestController automatically adds the @ResponseBody to your methods.

What @ResponseBody does is to bind the outgoing returned value to the HTTP response body using a HttpMessageConverter. If you don't add either the @RestController or @ResponseBody annotation then Spring will try to resolve that to a view, commonly a JSP page.

So in your case Spring is trying to find the view matchin "Hello"+customerId instead of printing out the result of "Hello"+customerId.

So you're using the @PathVariable annotation correctly. :)

You can read more here

0
votes

If you are using @Controller annotation then you need to add @ResponseBody annotation for binding the outgoing returned value to the HTTP response body. So your code with @Controller should look like:

@Controller
public class MainController 
{

    @RequestMapping("/user/{customerId}")
    @ResponseBody
    // http://localhost:8080/Test/user/5
    public ModelAndView getCustomerById(@PathVariable("customerId") String customerId, ModelAndView model) 
    {
            model.addAttribute("customer_id", customerId);
            // this is the user_details.jsp file, I need to show this jsp file to visitor
            model.setViewName("user_details");
            return model;
    }
}
0
votes

Please , check your application.properties file ...

prefix and suffix

spring.mvc.view.prefix: (Where are jsp files) Example /WEB-INF/ or / 
spring.mvc.view.suffix: .jsp