如何获得一个简单的Spring MVC视图(JSP)解决方案? [英] How to get a trivial case of Spring MVC view (JSP) resolving to work?
问题描述
我的应用程序使用Spring MVC(最新版本; 3.2.2)创建一个返回JSON的RESTful API,到目前为止我根本不需要视图层。但是现在,除了API之外,我还需要一个简单的实用程序页面(纯动态HTML),并希望使用JSP。
My app uses Spring MVC (latest; 3.2.2) to create a RESTful API returning JSON, and so far I haven't needed a view layer at all. But now, besides the API, I need a simple utility page (plain dynamic HTML) and wanted to use JSP for that.
我想要请求 http:// localhost:8080 / foo /< id>
通过控制器(Java)并最终进入JSP。应该简单吧?但我得到了404;解析视图时出现问题。
I want requests to http://localhost:8080/foo/<id>
to go through a controller (Java) and end up in a JSP. Should be simple, right? But I'm getting 404; something is not right in resolving the view.
HTTP ERROR 404
Problem accessing /jsp/foo.jsp. Reason:
Not Found
控制器:
@RequestMapping(value = "/foo/{id}")
public String testing(@PathVariable String id, ModelMap model) {
model.addAttribute("id", id);
return "foo";
}
定义控制器和映射请求有效;调用此方法就好了。
Defining controllers and mapping requests works; this method gets called just fine.
春季配置:
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/jsp/" p:suffix=".jsp"/>
问题可能在这里。我尝试了稍微不同的前缀,并将JSP放在 WEB-INF
下,以及< mvc:view-controller path = / */>
但还没有运气。
The problem is probably here. I've experimented with slightly different prefixes and putting the JSPs under WEB-INF
, as well as stuff like <mvc:view-controller path="/*" />
but no luck yet.
(我甚至需要指定InternalResourceViewResolver,还是默认视图解析器要小心这个?)
(Do I even need to specify InternalResourceViewResolver, or should default view resolvers take care of this?)
JSP文件。在 src / main / webapp / jsp
下(该项目使用Maven约定)我显然有JSP。
JSP files. Under src/main/webapp/jsp
(the project uses Maven conventions) I obviously have the JSPs.
这个位置有问题吗?
web.xml :
<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>
我浏览过 Spring MVC文档,但我的问题可能过于简单和明显,很容易在那里找到帮助。 :-P
I have browsed through Spring MVC documentation, but my problem is probably too trivial and obvious to easily find help there. :-P
任何人都可以告诉我我做错了什么吗?
Can anyone enlighten me on what I'm doing wrong?
推荐答案
我认为你需要做的是改变
I think what you need to do is changing
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
到
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
/ *
如果有,则不匹配是路径中的另一个文件夹,如 /jsp/foo.jsp
。另一方面, /
将匹配所有内容。
/*
won't match if there is another folder in the path, like /jsp/foo.jsp
. On the other hand /
will match everything.
这篇关于如何获得一个简单的Spring MVC视图(JSP)解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!