Can Spring MVC controller set the URL to be shown in browser?

时间:2016-04-15 14:48:23

标签: spring-mvc url-mapping

We have a Spring MVC Controller method which servers the url pattern /hello/{userName} .

@RequestMapping("/hello/{userName}")  
    public ModelAndView helloWorld(@PathVariable("userName") String productId) {  
        String message = "HELLO"+userName;  
        return new ModelAndView("hellopage", "message", message);  
    }  

Here when we request /hello/Tom ,hellopage.html will be servered with URL in the browser http://localhost:8080/myApp/hello/Tom

We would need the URL to be http://localhost:8080/myApp/Tom .Is there any way I can set the URL to be shown in the browser when returning from the controller.

2 个答案:

答案 0 :(得分:1)

当然,您可以使用assert*执行此操作。写两个控制器:

redirect

答案 1 :(得分:0)

我认为您也可以使用tuckey url-rewrite:http://tuckey.org/urlrewrite/

这允许Tomcat中的Apache mod_rewrite功能通过使用过滤器,您不必为所有内容编写两个控制器。