java - 使用 spring mvc 将 url 模式重定向到特定 Controller

标签 java spring-mvc

我想重定向这样的请求

localhost:8080/firstSpringProject/{uniqueusername}

到名为“profile”的特定 Controller :

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

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

    return "profile";
}

我正在使用 spring mvc;我该如何解决这种情况,还有其他方法吗?

最佳答案

Spring 文档在 redirect view 上说:

Note that URI template variables from the present request are automatically made available when expanding a redirect URL and do not need to be added explicitly neither through Model nor RedirectAttributes. For example:

@RequestMapping(value = "/files/{path}", method = RequestMethod.POST)
public String upload(...) {
    // ...
    return "redirect:files/{path}";
}

请记住,低于 3.1.4 的版本会受到 memory leak due to caching redirect views 的影响。 .

如果您使用的是 Spring 3.1.3 或更低版本并且您正在这样做

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

你会看到OutOfMemoryError sometime .

关于java - 使用 spring mvc 将 url 模式重定向到特定 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23320419/

相关文章:

java - 在Java中将对象序列化为json?

java - 什么是 Java 类加载器?

java - 在Spring应用程序中记录容器启动时的静态端点

java - 我可以使用在 DispatcherServlet 上下文中声明的 Hibernate session 工厂而不是 hibernate.cfg.xml 吗?

javascript - 使用 Javascript/jQuery 反序列化 Jackson 对象

java - 使用 Camera2 拍摄第二张照片时 setPreviewTexture 失败

java - 如何将字符串值转换为数字

java - 热衷于在 Java 中使用 Kafka Streams 创建差异流?

java - 从 Spring MVC 中的 Controller 操作重定向到 JSP 文件

spring - 所有 Spring-MVC Controller 的 ALPS 元数据