2016-06-10 63 views
2

我是Spring MVC的新手。我有基本的Rest Controller和JSP。我希望能够从JSP中访问请求映射注释中的URL。我注意到Spring MVC 4.1中的mvcUrl允许你这样做。但是当我尝试这个时遇到错误:SpringMVC - 错误使用mvcUrl - 缺少WebApplicationContext

当我打开localhost/test.jsp。我看到这个在浏览器:

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) 
    .... 

下面是我用

@RestController 
@RequestMapping("/foo") 
public class FooRestController { 
    @RequestMapping(value = "/hello", method = RequestMethod.GET) 
    public String hello() { 
      return "hello"; 
    } 
} 

JSP文件中的一些示例代码 - 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> 

可能是什么设置错了吗?

当我打localhost/foo/hello我确实收到一个json字符串"hello"。这是不是意味着调度程序servlet工作正常,并且WebApplicationContext实际设置正确?

+0

如果它返回“你好”,那么你可以说“你好”,不需要编码。 –

+0

不,这意味着你应该使用'@ Controller'而不是'@ RestController'。 – kryger

+0

我不知道我理解你。 – Jaskirat

回答

2

这里的问题是:您只为/foo/hello定义了控制器处理程序,但是没有为/test.jsp定义控制器处理程序。尝试添加一个新的控制器:

@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 
    } 
} 

,使存在于您的配置确保类似的代码,以便系统知道在哪里寻找你的观点

<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> 
+1

同意的错误来自于:断言WebApplicationContext == null,因为它无法解析源文件视图 – Destrif

+0

非常感谢您使用MvcUriComponentsBuilder.java。你是对的。我早些时候尝试过。因为我意识到jsp文件没有被Spring MVC正确渲染,也许是因为我从来没有通过调度器servlet运行它,而只是简单地为它服务。但是,那时候,我仍然得到了同样的错误。不知道我做错了什么。再次感谢! – Jaskirat

+1

写得很好的答案。接受这个。你得到赏金。 – Jaskirat

0

我不认为这是一个配置问题我们也面临类似的问题,唯一的解决方案是使用modelandview而不是返回视图作为控制器中的字符串

1

这与您使用的配置文件不存在问题 @RestController annotaion i n您的控制器,它是一个便利的注释,它本身用@Controller和@ResponseBody注解。正如你可能知道使用@ResponseBody注释你的类将会返回来自coltroller的方法的json响应,这是在这种情况下的预期行为。

如果你想获得JSP文件,你应该使用什么

1.使用@Controller代替@RestController 2.Assuming你已经配置视图解析器使用前缀和后缀的jsp页面并返回测试从该方法的字符串,因为您的jsp文件名称说它的名称是测试。

希望这回答你的问题。

+0

很好的答案。 (+1)你是对的。 view/jsp需要通过spring servlet解决,而不是仅仅使用默认的tomcat servlet来提供。 – Jaskirat