简体   繁体   English

Spring:Controller @RequestMapping到jsp

[英]Spring: Controller @RequestMapping to jsp

Using Spring 3.1.2 使用Spring 3.1.2

How do you reference the Annotated value of the CONTROLLER (not method) RequestMapping from a jsp so I can build URL's relative to the Controller. 如何从jsp引用CONTROLLER(非方法)RequestMapping的Annotated值,这样我就可以构建相对于Controller的URL。 If the method level request mappings are referenced, it will cause my links to not work, so they cannot be part of the this in any way. 如果引用了方法级别请求映射,则会导致我的链接不起作用,因此它们不能以任何方式成为其中的一部分。

For example (Note that this controller's mapping is "/foo/test"): 例如(请注意,此控制器的映射为“/ 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";
    }
}

...and from my this.jsp: ......并从我的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>

The reason I need to access the RequestMapping is because my views may be accessed from multiple Controllers. 我需要访问RequestMapping的原因是因为我的视图可以从多个控制器访问。 Note that this controller's mapping is "/bar/test"! 请注意,此控制器的映射是“/ 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";
    }
}

Some of my Controller level request mappings have path variables too, so getting just the string value of the request mapping will not work. 我的一些控制器级别请求映射也有路径变量,因此只获取请求映射的字符串值将不起作用。 It will have to be resolved: 它必须得到解决:

@Controller
@RequestMapping("/something/{anything}/test/")
public class SomethingController {
...
}

Update 更新

Maybe if there was a way to modify the context path by appending the Controller request mapping to it BEFORE the Spring URL tag, that would solve the problem. 也许如果有一种方法可以通过在Spring URL标记之前将Controller请求映射附加到它来修改上下文路径,那么这将解决问题。

contextPath = contextPath/controllerRequestMapping

Then, I could do something like this because I believe Spring will automatically retrieve the current context path: 然后,我可以这样做,因为我相信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>

This, of course, would be optimal! 当然,这将是最佳的! Ideas? 想法? Thoughts...? 思考......?

Thanks in advance! 提前致谢!

You could get the URI using the Servlet API. 您可以使用Servlet API获取URI。 There is no "Spring" way to do this as far as I know. 据我所知,没有“春天”的方法可以做到这一点。

@RequestMapping(value="/this", method=RequestMethod.GET)
public String getThis(HttpServletRequest request, Model m) {
    m.addAttribute("path", request.getRequestURI());
    return "test/this";
}

Then in your JSP: 然后在你的JSP中:

<a href="${path}">This</a>

For more information about HttpServletRequest, see the API here . 有关HttpServletRequest的更多信息, 请参阅此处的API

没有内置的方法来访问@RequestMapping ,但您只需将URL添加到模型中,然后从该视图中引用它。

According to me there is no difference in putting @RequestMapping annotation at both class level and method level. 据我所知,在类级别和方法级别上放置@RequestMapping注释没有区别。 Instead of that you can have the combined @RequestMapping annotation on top of the method only. 除此之外,您只能在方法之上使用组合的@RequestMapping注释。

So now your method level annotation will be something like this. 所以现在您的方法级别注释将是这样的。

@RequestMapping(value="("/foo/test/this", method=RequestMethod.GET)

or 要么

@RequestMapping(value="("/bar/test/this", method=RequestMethod.GET)

Now since both your methods return the same view you can have just one method for both of them. 现在,由于两个方法都返回相同的视图,因此两个方法只能有一个方法。 And in the annotation mapping value instead of foo and bar you can inroduce one path variable {from} . 在注释映射值而不是foo和bar中,您可以生成一个路径变量{from} So finally your annotation and the method will be like this. 所以最后你的注释和方法将是这样的。

@RequestMapping(value="("/{from}/test/this", method=RequestMethod.GET)
public String getThis(@PathVariable("from") String from) {       
    return "test/this";       
}

And after doing this in your method you can perform different calculations or operations on based of the runtime value of path variable from you get. 而在你的方法,这样做后,你可以在基于路径变量的运行时值你执行不同的计算或操作。 And in the JSP page you can simple get this value with ${from} . 在JSP页面中,您可以使用${from}简单地获取此值。

Hope this helps you. 希望这对你有所帮助。 Cheers. 干杯。

See code below to access request mapping, if you are using Spring 3.1 and above. 如果您使用的是Spring 3.1及更高版本,请参阅下面的代码以访问请求映射。 I'm not sure if this is what you require. 我不确定这是否是你所需要的。 This is just an attempt and of course i do not know of any straight forward way. 这只是一种尝试,当然我不知道任何直接的方式。 Invoke getRequestMappings() from inside a method that has request mapping. 从具有请求映射的方法内部调用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>();
    }



}

As of 4.1 every @RequestMapping is assigned a default name based on the capital letters of the class and the full method name. 从4.1开始,每个@RequestMapping都会根据类的大写字母和完整的方法名称分配一个默认名称。 For example, the method getFoo in class FooController is assigned the name "FC#getFoo". 例如,类FooController中的方法getFoo被赋予名称“FC#getFoo”。 This naming strategy is enter code here pluggable by implementing HandlerMethodMappingNamingStrategy and configuring it on your RequestMappingHandlerMapping. 这个命名策略是通过实现HandlerMethodMappingNamingStrategy并在RequestMappingHandlerMapping上配置它来enter code here可插入的enter code here Furthermore the @RequestMapping annotation includes a name attribute that can be used to override the default strategy. 此外,@ RequestMapping注释包含一个名称属性,可用于覆盖默认策略。 The Spring JSP tag library provides a function called mvcUrl that can be used to prepare links to controller methods based on this mechanism. Spring JSP标记库提供了一个名为mvcUrl的函数,该函数可用于根据此机制准备指向控制器方法的链接。

For example given: 例如给出:

@RequestMapping("/people/{id}/addresses")
public class MyController {
@RequestMapping("/{country}")
public HttpEntity getAddress(@PathVariable String country) { ... }
}

The following JSP code can prepare a link: 以下JSP代码可以准备链接:

<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
...
<a href="${s:mvcUrl('MC#getPerson').arg(0,'US').buildAndExpand('123')}">Get Address</a>

Make sure you have configured your MVC using either Java Config or XML way and not both the ways. 确保使用Java Config或XML方式配置MVC,而不是两种方式。 You will get an exception as :more than one RequestMappingInfo HandlerMapping found. 您将获得以下异常:找到多个RequestMappingInfo HandlerMapping。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM