2012-12-05 33 views
2

我在Spring MVC世界中頗爲新穎,我爲你解答了一個問題。Spring MVC中控制器方法的參數

我已經通過STS模板創建了一個新的Spring MVC項目。

所以現在我有一個很小的項目,一旦被執行,顯示出與顯示

當前日期和時間的Hello World消息這是唯一的控制器類此項目的代碼:

package org.gnagna.bla; 

import java.text.DateFormat; 
import java.util.Date; 
import java.util.Locale; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

/** 
* Handles requests for the application home page. 
*/ 
@Controller 
public class HomeController { 

private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

/** 
* Simply selects the home view to render by returning its name. 
*/ 
@RequestMapping(value = "/", method = RequestMethod.GET) 
public String home(Locale locale, Model model) { 
    logger.info("Welcome home! The client locale is {}.", locale); 

    Date date = new Date(); 
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 

    String formattedDate = dateFormat.format(date); 

    model.addAttribute("serverTime", formattedDate); 

    return "home"; 
} 

} 

所以只有一個方法叫做處理HTTP GET調用「/」的URL,好吧,這對我來說很明顯。

的事情,這不是clead是關於這個方法的輸入參數,因爲你可以看到這個方法接收兩個輸入參數:

1)模型

2)包含Locale對象一些關於用戶的信息

但是:誰創建了這個Locale對象,並且當它在我的控制器中作爲輸入參數傳遞?

TNX

安德烈

+0

這與你的問題沒有任何關係,但是java約定規定方法名應該是動詞http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html – Bal

回答

4

和LocaleResolver就是幹這個。

HandlerMethodInvokere最終使用一個這樣的解析器來檢查您正在調用的@RequestMapping方法的參數(在您的情況下爲「home」方法)。當它遇到一個Locale參數時,它只是使用區域設置解析器來獲取一個Locale對象作爲參數傳遞給它。

你可以在這裏看到關於它的更多信息: http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch16s06.html

http://templth.wordpress.com/2010/07/21/configuring-locale-switching-with-spring-mvc-3/

http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/

+0

mmm ...我有alredy閱讀一些有關Spring應用程序國際化的教程......但在這種情況下,我沒有在servlet-context.xml和web.xml文件中聲明LocalResolver ...爲什麼? – AndreaNobili

+1

如果您沒有指定任何區域設置解析器,Spring只是默認爲[AcceptHeaderLocaleResolver](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/i18n /AcceptHeaderLocaleResolver.html)。所以你總是有一個區域解析器。 –

+0

好吧,如果我已經很好地理解了它是如何工作的,請告訴我 Spring默認給我一個本地解析器,這看起來在我的HTTP請求的頭部內,因此知道我的Locale對象中存儲的「locality」作爲我的控制器方法的輸入參數在其中使用... 是不是正確? 謝謝 – AndreaNobili

1

在您的GET請求,您可以通過使用要求的特殊paramerter像發送本地信息到服務器, ?language=en。但問題是你的servlet容器如何知道language參數是帶有你的語言環境信息的參數。爲了讓Spring MVC的容器瞭解這個特殊的語言環境參數,你需要兩個附加的特殊攔截器在targer-servlet.xml像波紋管:

<bean id="localeChangeInterceptor" 
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
    <property name="paramName" value="language" /> 
</bean> 

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" > 
    <property name="interceptors"> 
     <list> 
     <ref bean="localeChangeInterceptor" /> 
     </list> 
    </property> 
</bean> 

的事情,現在,你的servlet容器知道與通過現場參數的值您的GET請求。但仍然需要有人根據所選語言鏈接更改區域設置,並相應地顯示屬性文件中的消息。命名爲localeResolver一個特殊的bean來救援做工作,像波紋管:

<bean id="localeResolver" 
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> 
    <property name="defaultLocale" value="en" /> 
</bean> 

現在,如果你想使用的語言環境對象的GET請求處理方法,你寫這樣public String home(Locale locale, Model model)方法簽名(如,你做了什麼)。然後你的servlet容器在調用它之前得到一個額外的工作,把這個locale對象作爲你的方法參數,並且你在你的方法裏面得到它像魔術一樣!