±à¼ÍƼö: |
±¾ÎÄÀ´×ÔÓÚ²©¿ÍÔ°£¬Spring MVCÊôÓÚSpringFrameWorkµÄºóÐø²úÆ·£¬ÒѾÈÚºÏÔÚSpring Web FlowÀïÃæ¡£Spring ¿ò¼ÜÌṩÁ˹¹½¨ Web Ó¦ÓóÌÐòµÄÈ«¹¦ÄÜ MVC Ä£¿é¡£ |
|
ÔÚMVC¿ò¼ÜÖУ¬¿ØÖÆÆ÷(Controller)ÓÃÓÚÖ´ÐÐÒµÎñÂß¼²¢²úÉúÄ£ÐÍÊý¾Ý(Model)£¬¶øÊÓͼ(View)ÔòÓÃÓÚäÖȾģÐÍÊý¾Ý
×÷Ϊһ¸öMVC¿ò¼Ü£¬ËüÃǶ¼»á·â×°²¢ÌṩһЩ»ù±¾µÄ×é¼þºÍ¹¦ÄÜÒÔ±ã½â·Å³ÌÐòÔ±µÄË«ÊÖ:
--·Ö·¢ÇëÇóµÄǰ¶Ë¿ØÖÆÆ÷(SpringMVCÖеÄDispatcherServlet)
--´¦ÀíÇëÇóµÄÒµÎñ¿ØÖÆÆ÷(SpringMVCÖеÄController)
--ÇëÇóURIÓëÇëÇó´¦Àí·½·¨µÄÆ¥Åä(SpringMVCÖеÄHandlerMapping)
--ÇëÇó´¦Àí·½·¨µÄµ÷ÓÃ(SpringMVCÖеÄHandlerAdapter)
--ÀàÐÍת»»ÎÊÌâ ¡ª¡ª ǰºǫ́Êý¾ÝµÄÁ÷ת
--Êý¾ÝУÑé
--Òì³£ÅäÖÃ
--¹ú¼Ê»¯ºÍ±êÇ©¿â
--ÎļþÉÏ´«/ÏÂÔØ
SpringMVCµÄÖ´ÐÐÁ÷³Ì

1¡¢Óû§Ïò·þÎñÆ÷·¢ËÍÇëÇó£¬ÇëÇó±»Spring MVCµÄǰ¶Ë¿ØÖÆÆ÷DispatcherServlet½Ø»ñ£»
2¡¢DispatcherServlet¶ÔÇëÇóURL£¨Í³Ò»×ÊÔ´¶¨Î»·û£©½øÐнâÎö£¬µÃµ½URI£¨ÇëÇó×ÊÔ´±êʶ·û£©¡£È»ºó¸ù¾Ý¸ÃURI£¬µ÷ÓÃHandlerMapping»ñµÃ¸ÃHandlerÅäÖõÄËùÓÐÏà¹Ø¶ÔÏ󣬰üÀ¨Handler¶ÔÏóÒÔ¼°Handler¶ÔÏó¶ÔÓ¦µÄÀ¹½ØÆ÷£¬ÕâЩ¶ÔÏó»á±»·â×°µ½Ò»¸ö HandlerExecutionChain¶ÔÏó µ±Öзµ»Ø£»
3¡¢DispatcherServlet¸ù¾Ý»ñµÃµÄHandler£¬Ñ¡ÔñÒ»¸öºÏÊʵÄHandlerAdapter¡£Ò»¸öHandlerAdapter»á±»ÓÃÓÚ´¦Àí¶àÖÖ(Ò»Àà)Handler£¬²¢µ÷ÓÃHandlerʵ¼Ê´¦ÀíÇëÇóµÄ·½·¨£»
4¡¢ÔÚµ÷ÓÃHandlerʵ¼Ê´¦ÀíÇëÇóµÄ·½·¨Ö®Ç°£¬HandlerAdapter Ê×ÏÈ»á½áºÏÓû§ÅäÖöÔÇëÇóÏûÏ¢½øÐÐת»»(ÀýÈ磬½«JSON/XMLÇëÇóÏûϢת»»³ÉÒ»¸öJava¶ÔÏó)£¬È»ºóͨ¹ýDataBinder½«ÇëÇóÖеÄÄ£ÐÍÊý¾Ý°ó¶¨µ½Handler(Controller)¶ÔÓ¦µÄ´¦Àí·½·¨µÄ²ÎÊýÖС£ÔÚÏûϢת»»ºÍÊý¾Ý°ó¶¨¹ý³ÌÖУ¬Spring MVC»á×öһЩ¶îÍâµÄ´¦Àí£¬±ÈÈçÊý¾ÝÀàÐÍת»»¡¢Êý¾Ý¸ñʽ»¯¹¤×÷ºÍÊý¾ÝºÏ·¨ÐÔУÑéµÈ£»
5¡¢Handlerµ÷ÓÃÒµÎñÂß¼×é¼þÍê³É¶ÔÇëÇóµÄ´¦Àíºó£¬ÏòDispatcherServlet·µ»ØÒ»¸öModelAndView¶ÔÏó£¬ModelAndView¶ÔÏóÖÐÓ¦¸Ã°üº¬ÊÓͼÃû»òÕßÊÓͼÃûºÍÄ£ÐÍ£»
6¡¢DispatcherServlet¸ù¾Ý·µ»ØµÄModelAndView¶ÔÏó£¬Ñ¡ÔñÒ»¸öºÏÊʵÄViewResolver£¨ÊÓͼ½âÎöÆ÷£©·µ»Ø¸øDispatcherServlet£»
7¡¢DispatcherServletµ÷ÓÃÊÓͼ½âÎöÆ÷ViewResolver½áºÏModelÀ´äÖȾÊÓͼView£»
8¡¢DispatcherServlet½«ÊÓͼäÖȾ½á¹û·µ»Ø¸ø¿Í»§¶Ë¡£
ÔÚÒÔÉϰ˸ö²½ÖèÖУ¬DispatcherServlet¡¢HandlerMapping¡¢HandlerAdapterºÍViewResolverµÈºËÐÄ×é¼þÏ໥ÅäºÏÀ´Íê³ÉSpring MVC ÇëÇó-ÏìÓ¦µÄÕû¸ö¹¤×÷Á÷³Ì¡£ÕâЩºËÐÄ×é¼þËùÍê³ÉµÄ¹¤×÷¶Ô¿ª·¢ÕßÊÇ͸Ã÷µÄ£¬Ò²¾ÍÊÇ˵£¬¿ª·¢Õß²¢²»ÐèÒª¹ØÐÄÕâЩ×é¼þÊÇÈçºÎ¹¤×÷µÄ£¬¿ª·¢ÕßÖ»ÐèҪרעÔÚHandler(Controller)µ±ÖÐÍê³É¶ÔÇëÇóµÄÒµÎñÂß¼´¦Àí¼´¿É£¬ÕâÒ²ÕýÊÇMVC¿ò¼ÜµÄ¼ÛÖµÌåÏÖ¡£
2¡¢SpringMVCµÄÏûϢת»»Æ÷»úÖÆ£ºHttpMessageConveter
ÔÚµ÷ÓÃÄ£ÐÍ×é¼þ´¦ÀíÒµÎñÂ߼ʱ³£³£ÊÇÒÔÒ»¸ö¸öÓÐÒµÎñÒâÒåµÄ¶ÔÏóΪ´¦Àíά¶ÈµÄ£¬ÄÇôÔÚÇëÇóÏûÏ¢µ½´ïSpringMVCºÍÏìÓ¦ÏûÏ¢´ÓSpringMVC³öÈ¥µÄ¹ý³ÌÖоʹæÔÚÒ»¸öÏûϢת»»µÄÎÊÌ⣬¼´ÇëÇóÏûÏ¢(×Ö·û´®)µ½Java¶ÔÏóµÄת»»ÎÊÌâ¡£
ÔÚSpringMVCÖУ¬²ÉÓõÄÊÇHttpMessageConverter»úÖÆ¡£¾ßÌå¶øÑÔ£¬HttpMessageConveter¸ºÔð½«ÇëÇóÐÅϢת»»ÎªÒ»¸ö¶ÔÏ󣬲¢Í¨¹ýDataBinder×é¼þ½«¸Ã¶ÔÏó°ó¶¨ÇëÇó·½·¨µÄ²ÎÊýÖлòÊä³öΪÏìÓ¦ÐÅÏ¢¡£
ÔÚSpringMVCÖпÉÒÔʹÓÃ@RequestBodyºÍ@ResponseBodyÁ½¸ö×¢½â·Ö±ðÍê³ÉÇëÇóÏûÏ¢µ½¶ÔÏóºÍ¶ÔÏóµ½ÏìÓ¦ÏûÏ¢µÄת»»£¬¶øµ×²ãÕâÖÖÁé»îµÄÏûϢת»»»úÖÆ¾ÍÊÇÓÉHttpMessageConverterÖ§³ÖµÄ¡£
SpringMVCµÄÇëÇó´¦ÀíÁ÷³Ì¿É¸ÅÀ¨ÈçÏ£ºµ±SpringMVCÊÕµ½ÇëÇóʱ£¬Ç°¶Ë¿ØÖÆÆ÷DispatcherServlet»á¸ù¾ÝÇëÇóURIµ÷ÓÃHandlerMapping½«ÇëÇó·Ö·¢¸ø¾ßÓÐһϵÁÐÀ¹½ØÆ÷ºÍÒµÎñ¿ØÖÆÆ÷ControllerµÄHandlerExecutionChain¶ÔÏó£¬È»ºó¸ÃÇëÇó½«ÒÀ´Îͨ¹ý¸ÃÖ´ÐÐÁ´µÄ¸÷¸öÀ¹½ØÆ÷²¢×îÖÕµ½´ïÒµÎñ¿ØÖÆÆ÷Controller¡£ÔÚÒµÎñ¿ØÖÆÆ÷Controller´¦Àí¸ÃÇëÇóǰ£¬HandlerAdapter»á¶ÔÇëÇóÏûÏ¢×÷½øÒ»²½×ª»»ºÍ½âÎö²¢°ó¶¨µ½ÒµÎñ¿ØÖÆÆ÷ControllerµÄ¾ßÌåÇëÇó´¦Àí·½·¨ÉÏ£¬È»ºó¸Ã·½·¨¸ù¾Ý½áºÏÇëÇó²ÎÊýµ÷ÓÃһϵÁÐÒµÎñÂß¼×é¼þÈ¥´¦ÀíÇëÇ󣬲¢½«°üº¬Ä£ÐÍÊý¾ÝºÍ¾ßÌåÊÓͼµÄ´¦Àí½á¹û½»¸øÊÓͼ½âÎöÆ÷ViewResolver½øÐÐäÖȾ£¬×îÖÕDispatcherServlet½«ÊÓͼäÖȾ½á¹û·µ»Ø¸ø¿Í»§¶Ë¡£
web.xmlÖÐÅäÖÃǰ¶Ë¿ØÖÆÆ÷ DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- ÅäÖÃ springÉÏÏÂÎÄ -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- ¼àÌýËùÓеÄservletºÍfilter -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ÅäÖõÄǰ¶Ë¿ØÖÆÆ÷ -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- SpringMVCÅäÖÃÎļþ·¾¶ºÍÃû³ÆÉ趨 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<!-- µÚÒ»¸ö¼ÓÔØ -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<!-- À¹½ØËùÓÐÇëÇó -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app> |
ervlet-context.xmlÖÐÖ¸¶¨Â·¾¶ÅäÖÃSpringMVCµÄÅäÖÃÎļþ
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- ÅäÖÃmvcµÄ¸÷ÖÖbean(×ÊÔ´£¬ÊÓͼ) -->
<!-- ×¢½âÇý¶¯ -->
<annotation-driven />
<!-- ¾²Ì¬×ÊÔ´·ÃÎÊ -->
<resources mapping="/resources/**" location="/resources/" />
<!-- ÊÓͼ½âÎö -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- ×¢½âɨÃè°ü -->
<context:component-scan base-package="com.wph.springmvc" />
</beans:beans> |
root-context.xmlÖÐÖ¸¶¨Â·¾¶ÅäÖà ·ÇMVCµÄ×ÊÔ´ µÄÅäÖÃÎļþ
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- ÅäÖà ·ÇmvcµÄbean ±ÈÈç Database -->
</beans> |
SpringMVC Êý¾Ý°ó¶¨»úÖÆµÄÓ¦ÓãºÊ¹ÓÃ×¢½âÍê³ÉÇëÇó²ÎÊý°ó¶¨
- ´¦ÀíRequest URI ²¿·Ö£¨ÕâÀïÖ¸URI TemplateÖÐVariable£¬²»º¬QueryString²¿·Ö£©µÄ×¢½â£º @PathVariable;
- ´¦ÀíRequest Header²¿·ÖµÄ×¢½â£º @RequestHeader£¬ @CookieValue;
- ´¦ÀíRequest Body²¿·ÖµÄ×¢½â£º@RequestParam£¬@RequestBody;
- ´¦ÀíAttributeÀàÐ͵Ä×¢½â£º @SessionAttributes£¬ @ModelAttribute;
1¡¢@PathVariable
µ±Ê¹ÓÃ@RequestMapping URI template ÑùʽӳÉäʱ£¬ ¼´ someUrl/{paramId}, ÕâʱµÄparamId¿Éͨ¹ý @Pathvariable×¢½â°ó¶¨Ëü´«¹ýÀ´µÄÖµµ½·½·¨µÄ²ÎÊýÉÏ¡£
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
@RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
} |
ÉÏÃæ´úÂë°ÑURI templateÖбäÁ¿ownerIdµÄÖµºÍpetIdµÄÖµ£¬°ó¶¨µ½·½·¨µÄ²ÎÊýÉÏ¡£Èô·½·¨²ÎÊýÃû³ÆºÍÐèÒª°ó¶¨µÄuri templateÖбäÁ¿Ãû³Æ²»Ò»Ö£¬ÐèÒªÔÚ@PathVariable(¡°name¡±)Ö¸¶¨uri templateÖеÄÃû³Æ¡£
2¡¢@RequestHeader£¬@CookieValue
(1). @RequestHeader
@RequestHeader ×¢½â¿ÉÒÔ°ÑRequestÇëÇóheader²¿·ÖµÄÖµ°ó¶¨µ½·½·¨µÄ²ÎÊýÉÏ£¬ÈçÏÂËùʾ :
Request Header£º
// ÕâÊÇÒ»¸ö Request µÄheader²¿·Ö
Host localhost:8080
Accept text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300 |
ʹÓÃ@RequestHeader×¢½â»ñÈ¡Request HeaderÖеÄһЩ×ֶΣº
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
//...
} |
ÉÏÃæµÄ´úÂ뽫°Ñrequest header²¿·ÖµÄAccept-EncodingµÄÖµ°ó¶¨µ½²ÎÊýencodingÉÏ£¬ °ÑKeep-Alive headerµÄÖµ°ó¶¨µ½²ÎÊýkeepAliveÉÏ¡£
(2). @CookieValue
@CookieValue ¿ÉÒÔ°ÑRequest headerÖйØÓÚcookieµÄÖµ°ó¶¨µ½·½·¨µÄ²ÎÊýÉÏ£¬ÀýÈçÓÐÈçÏÂCookieÖµ£º
JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84 |
ͨ¹ýÏÂÁз½Ê½¾Í¿ÉÒÔ°ÑJSESSIONIDµÄÖµ°ó¶¨µ½²ÎÊýcookieÉÏ£¬ÈçÏ£º
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) {
//...
} |
3¡¢@RequestParam£¬@RequestBody£¬@ResponseBody
(1). @RequestParam
- ³£ÓÃÀ´´¦Àí¼òµ¥ÀàÐ͵İ󶨣¬Í¨¹ýRequest.getParameter() »ñÈ¡µÄString¿ÉÖ±½Óת»»Îª¼òµ¥ÀàÐ͵ÄÇé¿ö£¨ String¨C> ¼òµ¥ÀàÐ͵Äת»»²Ù×÷ÓÉConversionServiceÅäÖõÄת»»Æ÷À´Íê³É£©£»ÒòΪÆäÄÚ²¿Ê¹ÓÃrequest.getParameter()·½Ê½»ñÈ¡²ÎÊý£¬ËùÒÔ¿ÉÒÔ´¦Àíget·½Ê½ÖÐqueryStringµÄÖµ£¬Ò²¿ÉÒÔ´¦Àípost·½Ê½ÖÐbody dataµÄÖµ£»
- ÓÃÀ´´¦ÀíContent-Type: Ϊ application/x-www-form-urlencoded±àÂëµÄÄÚÈÝ£¬Ìá½»·½Ê½GET¡¢POST£»
@Controller
@RequestMapping("/pets")
@SessionAttributes("pet")
public class EditPetForm {
// ...
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
return "petForm";
}
// ... |
(2). @RequestBody
@RequestBodyͨ¹ýʹÓÃHandlerAdapterĬÈÏÅäÖõÄHttpMessageConvertersÀ´½âÎöRequestÇëÇóµÄBody²¿·ÖÊý¾Ý²¢½«ÏàÓ¦µÄÊý¾Ý°ó¶¨µ½ControllerÖз½·¨µÄ²ÎÊýÉÏ£¬Æä³£ÓÃÀ´´¦ÀíContent-Type²»ÊÇapplication/x-www-form-urlencoded±àÂëµÄÄÚÈÝ£¬ÀýÈçapplication/json£¬application/xmlµÈ¡£×¢Ò⣬requestµÄbody²¿·ÖµÄÊý¾Ý±àÂë¸ñʽÓÉheader²¿·ÖµÄContent-TypeÖ¸¶¨¡£@RequestBodyµÄ¾ßÌåʹÓó¡¾°ÈçÏ£º
1). GET¡¢POST·½Ê½Ìáʱ£¬¸ù¾ÝRequest Header Content-TypeµÄÖµÀ´ÅжÏ:
- application/x-www-form-urlencoded£¬ ¿ÉÑ¡£¨¼´·Ç±ØÐ룬ÒòΪÕâÖÖÇé¿öµÄÊý¾Ý@RequestParam, @ModelAttributeÒ²¿ÉÒÔ´¦Àí£¬µ±È»@RequestBodyÒ²ÄÜ´¦Àí£©£»
- multipart/form-data, ²»ÄÜ´¦Àí£¨¼´Ê¹ÓÃ@RequestBody²»ÄÜ´¦ÀíÕâÖÖ¸ñʽµÄÊý¾Ý£©£»
- ÆäËû¸ñʽ£¬ ±ØÐ루ÆäËû¸ñʽ°üÀ¨application/json£¬application/xmlµÈ¡£ÕâЩ¸ñʽµÄÊý¾Ý±ØÐëʹÓÃ@RequestBodyÀ´´¦Àí£©£»
2). PUT·½Ê½Ìύʱ£¬¸ù¾ÝRequest Header Content-TypeeµÄÖµÀ´ÅжÏ:
- application/x-www-form-urlencoded£¬ ±ØÐ룻
- multipart/form-data, ²»ÄÜ´¦Àí£»
- ÆäËû¸ñʽ£¬ ±ØÐ룻
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
} |
(3). @ResponseBody
@ResponseBody×¢½âÓÃÓÚ½«ControllerµÄ·½·¨·µ»ØµÄ¶ÔÏóͨ¹ýÊʵ±µÄHttpMessageConverterת»»ÎªÖ¸¶¨¸ñʽºó£¬Ð´Èëµ½Response¶ÔÏóµÄbodyÊý¾ÝÇø£¬ÆäÔÚ·µ»ØµÄÊý¾Ý²»ÊÇhtml±êÇ©µÄÒ³Ãæ£¬¶øÊÇÆäËûijÖÖ¸ñʽµÄÊý¾Ýʱ£¨Èçjson¡¢xmlµÈ£©Ê¹Óã¬ÀýÈ磺
@RequestMapping(value="/testRequestBody")
// ½«ControllerµÄ·½·¨·µ»ØµÄ¶ÔÏóͨ¹ýÊʵ±µÄÏûϢת»»Æ÷ת»»ÎªÖ¸¶¨¸ñʽºóдÈëµ½Response¶ÔÏóµÄbodyÊý¾ÝÇø
@ResponseBody
public Book setJson(@RequestBody Book book,
HttpServletResponse response) throws Exception{ // @RequestBody¸ù¾ÝjsonÊý¾Ý£¬×ª»»³É¶ÔÓ¦µÄObject
book.setAuthor("ФÎļª");
logger.info(JSONObject.toJSONString(book));
return book;
} |
4¡¢@SessionAttributes£¬@ModelAttribute
(1). @SessionAttributes
@SessionAttributes×¢½âÓÃÀ´°ó¶¨HttpSessionÖеÄattribute¶ÔÏóµÄÖµ£¬±ãÓÚÔÚ·½·¨ÖеIJÎÊýÀïʹÓã¬ÀýÈ磺
@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm {
// ...
} |
(2). @ModelAttribute
@ModelAttribute×¢½âÓÐÁ½¸öÓ÷¨£¬Ò»¸öÊÇÓÃÓÚ·½·¨ÉÏ£¬Ò»¸öÊÇÓÃÓÚ²ÎÊýÉÏ¡£ÓÃÓÚ·½·¨ÉÏʱ£¬±»Æä×¢Ê͵ķ½·¨»áÔÚControllerÿ¸ö·½·¨Ö´ÐÐǰ±»Ö´ÐУ¬Òò´Ëͨ³£ÓÃÀ´ÔÚ´¦Àí@RequestMapping֮ǰΪÇëÇó°ó¶¨ÐèÒª´Óºǫ́²éѯµÄmodel£»ÓÃÓÚ²ÎÊýÉÏʱ£¬ÓÃÀ´Í¨¹ýÃû³Æ¶ÔÓ¦°ÑÏàÓ¦Ãû³ÆµÄÖµ°ó¶¨µ½×¢½âµÄ²ÎÊýbeanÉÏ£¬ÆäÖÐÒª°ó¶¨µÄÖµ³£³£À´Ô´ÓÚ£º
- @SessionAttributes ÆôÓõÄattribute ¶ÔÏóÉÏ£»
- @ModelAttribute ÓÃÓÚ·½·¨ÉÏʱָ¶¨µÄmodel¶ÔÏó£»
@ModelAttributeÔÚ·½·¨ÉÏʹÓÃʾÀý£º
// Add one attribute
// The return value of the method is added to the model under the name "account"
// You can customize the name via @ModelAttribute("myAccount")
@ModelAttribute
public Account addAccount(@RequestParam String number) {
return accountManager.findAccount(number);
}
|
ÕâÖÖ·½Ê½Êµ¼ÊµÄЧ¹û¾ÍÊÇÔÚµ÷ÓÃ@RequestMappingµÄ·½·¨Ö®Ç°Ïòrequest¶ÔÏóµÄmodelÀïput£¨¡°account¡±£¬ Account£©¡£
@ModelAttributeÓÃÔÚ²ÎÊýÉϵÄʾÀý´úÂ룺
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) {
}
|
Ê×ÏȲéѯ @SessionAttributesÓÐÎް󶨵ÄPet¶ÔÏó£¬ÈôûÓÐÔò²éѯ@ModelAttribute·½·¨²ãÃæÉÏÊÇ·ñ°ó¶¨ÁËPet¶ÔÏó£¬ÈôûÓÐÔò½«URI templateÖеÄÖµ°´¶ÔÓ¦µÄÃû³Æ°ó¶¨µ½Pet¶ÔÏóµÄ¸÷ÊôÐÔÉÏ¡£
|