[英]How to harness url in Spring MVC?
在Web应用程序(在Eclipse中开发)中,我希望用户利用浏览器中的url。 Web应用程序基于Java Spring MVC,控制器返回html页面。 所有html页面都位于WebContent / WEB-INF / views文件夹中。 所有css \\ javacript \\ images都位于WebContent / resources / {css \\ javacript \\ images}文件夹中。
以下是此网络应用应访问的网址
现在我想为供应商实施类别过滤器
进一步在vendor.html(可能是{all,med,军队,其他}供应商)上,单击“名称”链接,并将网址设为
localhost:8080 / Project / vendor / med / vendor_XX来显示所选vendor_XX的完整信息-(编码在vendor_XX.html中)
所有提交均为GET类型
主页/关于/vendor_XX.html
<html>
<link rel="stylesheet" href="resources/css/mystyle.css" type="text/css" />
<a href="home">Home</a>
<a href="vendor">Vendor</a>
<a href="about">About</a>
<a href="vendor/med">Medical</a>
<a href="vendor/army">Army</a>
<a href="vendor/other">Other</a>
// and other non relevant stuff
</html>
vendor.html
<html>
<link rel="stylesheet" href="resources/css/mystyle.css" type="text/css" />
<a href="home">Home</a>
<a href="vendor">Vendor</a>
<a href="about">About</a>
<a href="vendor/med">Medical</a>
<a href="vendor/army">Army</a>
<a href="vendor/other">Other</a>
// generating below 3 line dynamically with js
<a href="vendor/med/vendor_xx">Vendor_XX</a>
<a href="vendor/med/vendor_yy">Vendor_YY</a>
<a href="vendor/other/vendor_zz">Vendor_ZZ</a>
// and other non relevant stuff
</html>
我的控制器
@Controller
public class AppController {
@RequestMapping(value = "home", method = RequestMethod.GET)
public String home() {
return "home";
}
@RequestMapping(value = "vendor", method = RequestMethod.GET)
public String vendor() {
return "vendor";
}
@RequestMapping(value = "vendor/med", method = RequestMethod.GET)
public String vendorMed() {
return "vendor";
}
@RequestMapping(value = "vendor/army", method = RequestMethod.GET)
public String vendorArmy() {
return "vendor";
}
@RequestMapping(value = "vendor/med/vendor_xx", method = RequestMethod.GET)
public String vendorMedXX() {
return "vendor_xx";
}
//all sample urls are given
}
添加资源文件夹以创建项目的路径
localhost:8080 / Project / vendor / med / vendor_XX在上述网址上将其视为localhost:8080 / Project / level_1 / level_2 / level_3
问题1)-未找到除level_1以外的所有URL的CSS。 level_2 url需要css导入为<link rel="stylesheet" href="../resources/css/mystyle.css" type="text/css" />
level_3 url需要css导入为<link rel="stylesheet" href="../../resources/css/mystyle.css" type="text/css" />
问题1-为什么不从资源中加载CSS。 我想念什么吗?
2)-万一我点击
<a href="home">Home</a>
从level_1 / level_2 vendor.html,它被定向到level_1 / home。 因此在控制器请求映射中找不到。
问题2-我们如何重定向到localhost:8080 / Project / home?
URL的工作方式非常类似于命令行中的路径。
如果您位于目录/foo/bar/
并执行命令less file.txt
,则将打开文件/foo/bar/file.txt
,因为您使用的是相对路径。
如果要打开文件/file.txt
,则需要less ../../file.txt
来进入两个目录,或者只需less /file.txt
即可从根目录开始。
当您访问其URL(即在浏览器的位置栏中看到的内容)为http://localhost/Project/vendor/med/vendor_xx
,将文件加载到resources/css/mystyle.css
,那么浏览器将从http://localhost/Project/vendor/med/resources/css/mystyle.css
加载它,因为您使用的是相对文件,并且当前的“目录”为http://localhost/Project/vendor/med/
。
要从正确的URL(即http://localhost/Project/resources/css/mystyle.css
加载它,您需要使用../../resources/css/mystyle.css
进入两个目录,或者您需要从根目录开始的绝对路径: /Project/resources/css/mystyle.css
。
为了避免对项目的上下文路径(即/Project
)进行硬编码,可以使用JSTL的c:url标记,或者简单地
href="${pageContext.request.contextPath}/resources/css/mystyle.css"
我强烈建议几乎总是使用上述绝对路径,无论您在何处,都可以使用绝对路径。
访问资源,您需要在配置类中添加ResourceHandler ...
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
对于网址,您可以使用spring网址标签:
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
<link rel="stylesheet" href="<spring:url value="/resources/css/mystyle.css" htmlEscape="true"/>" type="text/css" />
<a href="<spring:url value="/home"htmlEscape="true"/>" >Home</a>
// ....
</html>
用于重定向您可以使用“ redirect:”前缀:
return "redirect:/redirectPath";
网上有许多示例,您只需稍作搜索即可使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.