簡體   English   中英

Spring MVC URL格式

[英]spring mvc url formatting

我正在使用Spring MVC。 我的網站上有3個頁面(實際上是控制器處理了這些請求):

localhost/post.html
localhost/search.html
localhost/list.html

我希望該網址為localhost/XXX/post.html ,其中XXX是將作為參數傳遞給控制器​​方法的參數。 例如,如果用戶詢問localhost/bla/post.html/post的控制器方法將把bla作為參數。

在春季MVC中這可能嗎?

是的,而且很容易做到。 在定義處理程序URL的映射時,可以將變量放入路徑中,如下所示:

@RequestMapping("/{blah}/post.html")
public ModelAndView handleRequest(@PathVariable("blah") String blah) {
  //
}

Spring會在調用方法時為您設置String的值。

(雖然不是絕對必要,但在路徑中為調度程序servlet擁有一個子目錄也是正常的。將調度程序映射到根目錄可能會很麻煩。例如,訪問靜態資源。例如,localhost / myapp / blah / post.html)

在JSF中,可以使用PrettyFaces庫完成。 可能會對您有幫助。

使用批注很容易做到:

@Controller
public class MyController
{   

    @RequestMapping(value = "/{id}/post", method = RequestMethod.POST)
    public String post(@PathVariable("id") String id)
    {
    }

    @RequestMapping(value = "/{id}/search", method = RequestMethod.GET)
    public String search(@PathVariable("id") String id)
    {
    }

    @RequestMapping(value = "/{id}/list", method = RequestMethod.GET)
    public String list(@PathVariable("id") String id)
    {
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM