JSP中的Spring MVC请求URL [英] Spring MVC Request URLs in JSP

查看:246
本文介绍了JSP中的Spring MVC请求URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring MVC编写一个Web应用程序。我使用控制器的注释等。除非涉及到应用程序中的实际链接(表单动作,< a> 标签等),否则一切工作正常。 )当前,我有这个(显然缩写):

$ $ p $ $ $
@RequestMapping(value = / admin / listPeople,method = RequestMethod.GET)

//在JSP
< a href =/ admin / listPeople>转到人员列表< / a>

当我直接输入URL如http:// localhost:8080 / MyApp / admin / listPeople ,页面加载正确。但是,上面的链接不起作用。它丢失了应用程序名称MyApp。

有人知道是否有一种方法可以配置Spring在应用程序名称上?



让我知道是否需要查看我的任何Spring配置。我正在使用带有视图解析器的标准调度器servlet等。

解决方案

您需要在链接上添加上下文路径。 / p>

  //位于JSP顶部的某处
< c:set var =contextPathvalue =$ {pageContext.request.contextPath}/>

...
< a href =$ {contextPath} / admin / listPeople>转到人员列表< / a>


I am writing a web application using Spring MVC. I am using annotations for the controllers, etc. Everything is working fine, except when it comes to actual links in the application (form actions, <a> tags, etc.) Current, I have this (obviously abbreviated):

//In the controller
@RequestMapping(value="/admin/listPeople", method=RequestMethod.GET)

//In the JSP
<a href="/admin/listPeople">Go to People List</a>

When I directly enter the URL like "http://localhost:8080/MyApp/admin/listPeople", the page loads correctly. However, the link above does not work. It looses the application name "MyApp".

Does anyone know if there is a way to configure Spring to throw on the application name on there?

Let me know if you need to see any of my Spring configuration. I am using the standard dispatcher servlet with a view resolver, etc.

解决方案

You need to prepend context path to your links.

// somewhere on the top of your JSP
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

...
<a href="${contextPath}/admin/listPeople">Go to People List</a>

这篇关于JSP中的Spring MVC请求URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆