问题描述:

I am new to Spring MVC. I have basic Rest Controller and JSP. I want to be able to access the URL in the request mapping annotations from the JSP. I noticed that mvcUrl in Spring MVC 4.1 allows you to do that. But I run into an error when I try this:

When I open localhost/test.jsp. I see this in the browser:

org.apache.jasper.JasperException: javax.el.ELException: Problems calling function 's:mvcUrl'

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)

.....

java.lang.IllegalArgumentException: Cannot lookup handler method mappings without WebApplicationContext

org.springframework.util.Assert.notNull(Assert.java:115)

org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.getRequestMappingInfoHandlerMapping(MvcUriComponentsBuilder.java:521)

org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.fromMappingName(MvcUriComponentsBuilder.java:330)

org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.fromMappingName(MvcUriComponentsBuilder.java:313)

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

....

Here is some sample code that I used

@RestController

@RequestMapping("/foo")

public class FooRestController {

@RequestMapping(value = "/hello", method = RequestMethod.GET)

public String hello() {

return "hello";

}

}

JSP File - test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>

<a href="${s:mvcUrl('FRC#hello').build()}">Link to Hello</a>

What could be setup wrong?

When I hit localhost/foo/hello I do get back a json string "hello". Doesn't that mean the dispatcher servlet is working fine and the WebApplicationContext is actually setup properly?

网友答案:

The problem here is: you only defined controller handler for /foo/hello, but not for /test.jsp . Try adding a new controller:

@Controller
@RequestMapping("/")
public class GeneralController {
    ...

    @RequestMapping(value = "/test.jsp", method = RequestMethod.GET)
    public String hello() {
           return "test"; // put the view name here. You won't need ".jsp" because it would be automatically added by the view resolver in the configuration below
    }
}

and make sure similar code exists in your configuration, so that the system know where to look for your view

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/[Your_JSP_FOLDER]"/>
    <property name="suffix" value=".jsp"/>
</bean> 
网友答案:

This not a problem with your configuration file as you are using @RestController annotaion in your controller which is a convenience annotation that is itself annotated with @Controller and @ResponseBody . as you may know annotating your class with @ResponseBody will return you back the json response from the method of the coltroller which is the expected behavior in this case.

If you want to get the jsp file what you should have used is

1.use @Controller instead of @RestController 2.Assuming you have configured view resolver with prefix and suffix for jsp page and return test string from the method because your jsp file name says it's name is test .

Hope this answer your question.

网友答案:

I don't think this is a configuration issue we have also faced similar kind of issues the only solution was to use modelandview instead of returning the view as string in the controller

相关阅读:
Top