如何处理包含正斜杠 (/) 的请求? [英] How to handle requests that includes forward slashes (/)?

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

问题描述

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

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";
    }
}

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

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