2012-08-17 33 views
0

我是spring mvc3開發的新手,並且面臨一個小問題(我沒有面對ASP.Net MVC3)。我想知道爲控制器定義默認(或着陸)URL的過程。定義控制器彈簧的默認網址mvc3

我有一個帳戶控制器,我做所有帳戶管理相關的東西。所以我所有的url都映射到這個控制器。我想知道如何將我的「/ accounts」url請求映射到openAccountsDashboard方法?

碼 -

.... imports... 

@Controller 
@RequestMapping(value = "/accounts/*") 
public class AccountController { 

     @RequestMapping(value = "/", method = RequestMethod.GET) 
     public ModelAndView openAccountsDashboard(HttpServletRequest request) { 
      ..... 
      return new ModelAndView("accounts/landing"); 
     } 

     @RequestMapping(value = "/change-password", method = RequestMethod.GET) 
     public ModelAndView openPasswordChangePage(HttpServletRequest request) { 
      ..... 
      return new ModelAndView("accounts/passwordChange"); 
     } 
... other actions... 

} 

任何幫助將是巨大的!

感謝

+0

當您刪除openAccountsDashboard的attr值時會發生什麼情況? – 2012-08-17 05:43:12

+0

它只是當我使用url - '/ accounts /'而不是'/ accounts'(給404) – saarthak 2012-08-17 06:14:55

+0

時,爲什麼不刪除/ *,只是在類級別使用/ accounts,並在方法中添加自定義路徑級別/更改密碼 – 2012-08-17 07:23:51

回答

0

嘗試這樣:

.... imports... 

@Controller 
@RequestMapping(value = "/accounts/") 
public class AccountController { 

     @RequestMapping(value = "", method = RequestMethod.GET) 
     public ModelAndView openAccountsDashboard(HttpServletRequest request) { 
      ..... 
      return new ModelAndView("accounts/landing"); 
     } 

     @RequestMapping(value = "/change-password", method = RequestMethod.GET) 
     public ModelAndView openPasswordChangePage(HttpServletRequest request) { 
      ..... 
      return new ModelAndView("accounts/passwordChange"); 
     } 
... other actions... 

} 

然後你可以使用的URL是這樣的:

http://localhost:8080/yourwebapp/accounts/ 

打openAccountsDashboard方法。 Regards, Arek

+0

它擊中的方法,但只有當我使用url - '/ accounts /'而不是與'/帳戶'(給404) – saarthak 2012-08-17 07:12:45

+1

見13.11.3 static.springsource.org/spring/docs/2.5.4/reference/mvc.html – 2012-08-17 07:37:51

+0

但是,這個問題與URL結束'/'或不?我認爲這是合乎邏輯的,我們有一些特定控制器的URL前綴,如果你想從這個控制器中選擇方法 - 你應該使用整個前綴。如果你想通過傳遞像'localhost:8080/youtwebapp/accounts'這樣的URL來實現這個方法,那麼你應該將你的整個控制器映射到'/',然後將方法openAccountsDashboard映射到'accounts',這不是優雅的(在我看來),因爲這種方法與控制器邏輯產生混雜。 – 2012-08-17 07:49:09