[英]Spring: Controller @RequestMapping to jsp
使用Spring 3.1.2
如何從jsp引用CONTROLLER(非方法)RequestMapping的Annotated值,這樣我就可以構建相對於Controller的URL。 如果引用了方法級別請求映射,則會導致我的鏈接不起作用,因此它們不能以任何方式成為其中的一部分。
例如(請注意,此控制器的映射為“/ foo / test”):
@Controller
@RequestMapping("/foo/test")
public class FooController {
@RequestMapping(value="/this", method=RequestMethod.GET)
public String getThis() {
return "test/this";
}
@RequestMapping(value="/that", method=RequestMethod.GET)
public String getThat() {
return "test/that";
}
@RequestMapping(value="/other", method=RequestMethod.GET)
public String getOther() {
return "test/other";
}
}
......並從我的this.jsp:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<s:url value="${controllerRequestMapping}" var="baseUrl"/>
<a href="${baseUrl}/that">That</a>
<a href="${baseUrl}/other">Other</a>
我需要訪問RequestMapping的原因是因為我的視圖可以從多個控制器訪問。 請注意,此控制器的映射是“/ bar / test”!
@Controller
@RequestMapping("/bar/test")
public class BarController {
@RequestMapping(value="/this", method=RequestMethod.GET)
public String getThis() {
return "test/this";
}
@RequestMapping(value="/that", method=RequestMethod.GET)
public String getThat() {
return "test/that";
}
@RequestMapping(value="/other", method=RequestMethod.GET)
public String getOther() {
return "test/other";
}
}
我的一些控制器級別請求映射也有路徑變量,因此只獲取請求映射的字符串值將不起作用。 它必須得到解決:
@Controller
@RequestMapping("/something/{anything}/test/")
public class SomethingController {
...
}
更新
也許如果有一種方法可以通過在Spring URL標記之前將Controller請求映射附加到它來修改上下文路徑,那么這將解決問題。
contextPath = contextPath/controllerRequestMapping
然后,我可以這樣做,因為我相信Spring會自動檢索當前的上下文路徑:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<a href="<s:url value="/that"/>">That</a>
<a href="<s:url value="/other"/>">Other</a>
當然,這將是最佳的! 想法? 思考......?
提前致謝!
您可以使用Servlet API獲取URI。 據我所知,沒有“春天”的方法可以做到這一點。
@RequestMapping(value="/this", method=RequestMethod.GET)
public String getThis(HttpServletRequest request, Model m) {
m.addAttribute("path", request.getRequestURI());
return "test/this";
}
然后在你的JSP中:
<a href="${path}">This</a>
有關HttpServletRequest的更多信息, 請參閱此處的API 。
沒有內置的方法來訪問@RequestMapping
,但您只需將URL添加到模型中,然后從該視圖中引用它。
據我所知,在類級別和方法級別上放置@RequestMapping注釋沒有區別。 除此之外,您只能在方法之上使用組合的@RequestMapping注釋。
所以現在您的方法級別注釋將是這樣的。
@RequestMapping(value="("/foo/test/this", method=RequestMethod.GET)
要么
@RequestMapping(value="("/bar/test/this", method=RequestMethod.GET)
現在,由於兩個方法都返回相同的視圖,因此兩個方法只能有一個方法。 在注釋映射值而不是foo和bar中,您可以生成一個路徑變量{from} 。 所以最后你的注釋和方法將是這樣的。
@RequestMapping(value="("/{from}/test/this", method=RequestMethod.GET)
public String getThis(@PathVariable("from") String from) {
return "test/this";
}
而在你的方法,這樣做后,你可以在基於路徑變量的運行時值從你執行不同的計算或操作。 在JSP頁面中,您可以使用${from}
簡單地獲取此值。
希望這對你有所幫助。 干杯。
如果您使用的是Spring 3.1及更高版本,請參閱下面的代碼以訪問請求映射。 我不確定這是否是你所需要的。 這只是一種嘗試,當然我不知道任何直接的方式。 從具有請求映射的方法內部調用getRequestMappings()。
public class FooController{
private RequestMappingHandlerMapping handlerMapping = null;
@Autowired
public FooController(RequestMappingHandlerMapping handlerMapping){
this.handlerMapping = handlerMapping;
}
public Set<String> getRequestMappings(){
Map<RequestMappingInfo, HandlerMethod> handlerMap = handlerMapping.getHandlerMethods();
Iterator<RequestMappingInfo> itr = handlerMap.keySet().iterator();
while(itr.hasNext()){
RequestMappingInfo info = itr.next();
PatternsRequestCondition condition = info.getPatternsCondition();
Set<String> paths = condition.getPatterns();
HandlerMethod method = handlerMap.get(info);
if(method.getMethod().getName().equals(Thread.currentThread().getStackTrace()[2].getMethodName()))
return paths;
}
return new HashSet<String>();
}
}
從4.1開始,每個@RequestMapping都會根據類的大寫字母和完整的方法名稱分配一個默認名稱。 例如,類FooController中的方法getFoo被賦予名稱“FC#getFoo”。 這個命名策略是通過實現HandlerMethodMappingNamingStrategy並在RequestMappingHandlerMapping上配置它來enter code here
可插入的enter code here
。 此外,@ RequestMapping注釋包含一個名稱屬性,可用於覆蓋默認策略。 Spring JSP標記庫提供了一個名為mvcUrl的函數,該函數可用於根據此機制准備指向控制器方法的鏈接。
例如給出:
@RequestMapping("/people/{id}/addresses")
public class MyController {
@RequestMapping("/{country}")
public HttpEntity getAddress(@PathVariable String country) { ... }
}
以下JSP代碼可以准備鏈接:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
...
<a href="${s:mvcUrl('MC#getPerson').arg(0,'US').buildAndExpand('123')}">Get Address</a>
確保使用Java Config或XML方式配置MVC,而不是兩種方式。 您將獲得以下異常:找到多個RequestMappingInfo HandlerMapping。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.