本文介绍了如何处理包含正斜杠(/)的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按以下方式处理请求:

I need to handle requests as following:

www.example.com/show/abcd/efg?name=alex&family=moore   (does not work)
www.example.com/show/abcdefg?name=alex&family=moore   (works)
www.example.com/show/abcd-efg?name=alex&family=moore   (works)

它应接受来自值的任何类型的字符位于 www.example.com/show / 之间。请注意,那里的值将是单个值而不是操作的名称。

It should accept any sort of character from the value that is located between www.example.com/show/ and ?. Please note the value that would be located there would be a single value not name of an action.

例如: / show / abcd / efg / show / lkikf?name = Jack 其中第一个请求应将用户重定向到页面 abcd / efg (因为那是一个名字),第二个应该将用户重定向到页面 lkikf 以及参数名称的值。

For example: /show/abcd/efg and /show/lkikf?name=Jack in which the first request should redirect user to the page abcd/efg (because thats a name) and the second one should redirect user to the page lkikf along with value of parameter name.

我有以下控制器来处理它,但问题是我在地址中控制器无法处理它。

I have following controller to handle it but the issue is when I have / in the address the controller is unable to handle it.

@RequestMapping(value = "/{mystring:.*}", method = RequestMethod.GET)
public String handleReqShow(
            @PathVariable String mystring,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String family, Model model)     {

我使用了以下正则表达式无效。

I used following regex which did not work.

 /^[ A-Za-z0-9_@./#&+-]*$/


推荐答案

你必须创建两个方法,然后创建一个 @RequestMapping(value = {/ {string:。+}}} 注释,另一个方法具有 @RequestMapping (value = {/ {string:。+},/ {string:。+}} {mystring:。+}}} 然后相应地采取行动,因为你可以'有可选的

You have to create two methods then one having the @RequestMapping(value = { "/{string:.+}" }) annotation and the other having @RequestMapping(value = { "/{string:.+}", "/{string:.+}/{mystring:.+}" }) and then act accordingly in each, because you can't have optional path variables.

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/show")
public class HelloController {

    @RequestMapping(value = { "/{string:.+}" })
    public String handleReqShow(@PathVariable String string,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String family, Model model) {
        System.out.println(string);
        model.addAttribute("message", "I am called!");
        return "hello";
    }

    @RequestMapping(value = { "/{string:.+}", "/{string:.+}/{mystring:.+}" })
    public String whatever(@PathVariable String string,
            @PathVariable String mystring,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String family, Model model) {
        System.out.println(string);
        System.out.println(mystring);
        model.addAttribute("message", "I am called!");
        return "hello";
    }
}

这篇关于如何处理包含正斜杠(/)的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 11:27