Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SpringMVC将url模式重定向到特定的控制器_Java_Spring Mvc - Fatal编程技术网

Java 使用SpringMVC将url模式重定向到特定的控制器

Java 使用SpringMVC将url模式重定向到特定的控制器,java,spring-mvc,Java,Spring Mvc,我想重定向这样的请求 localhost:8080/firstSpringProject/{uniqueusername} 到名为“profile”的特定控制器: @RequestMapping(value="/profile") public String profiles(Model model){ based on the uniqueusername i would like to render a profile page return "profile"; }

我想重定向这样的请求

localhost:8080/firstSpringProject/{uniqueusername}

到名为“profile”的特定控制器:

@RequestMapping(value="/profile")
public String profiles(Model model){

    based on the uniqueusername i would like to render a profile page 

    return "profile";
}

我使用的是SpringMVC;如何解决这种情况还有其他方法吗?

我想您可以在这里使用spring路径变量。您必须创建一个控制器方法,该方法将根据您的URL要求使用username,并将重定向到带有username参数的profile方法

    @RequestMapping(value="/{username}")
    public String getUserName(Model model,@PathVariable("username") String username){

        //process username here and then redirect to ur profile method

        return "redirect : profile?username="+username;
    }

    @RequestMapping(value="/profile")
    public String profiles(Model model,String username){

        //have a username and render a profile page 

        return "profile";
    }

感谢您

Spring文档中提到:

请注意,来自当前请求的URI模板变量是 展开重定向URL时自动可用,且不 需要通过模型或 重定向属性。例如:

请记住,低于3.1.4的版本会受到影响

如果您正在使用Spring 3.1.3或更低版本,并且正在执行此操作

return "redirect : profile?username="+username;

您将看到。

不清楚源模式是什么。你的意思是像
@RequestMapping(“/{uniqueUserName}”)
这样的东西吗?sourece请求可能是这样的:firstSpringProject/username1或firstSpringProject/username2左右,我想将其重新记录到@RequestMapping(value=“/profile”)中,然后基于我想呈现用户配置文件的url(这里可能是用户名1或用户名2)
return "redirect : profile?username="+username;