First, Spring MVC framework Construction steps
1. Importing JAR files
Spring-web-3.2.0.release
Spring-webmvc-3.2.0.release
2. Configuring Servlets and other parameters in Web. xml
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "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"> <Display-name></Display-name> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list> <!--Configuring the front-end controller in Web. XML, Dispatcherservlet is the spring's most and core class - <servlet> <Servlet-name>Dispatcherservlet</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:applicationContext.xml</Param-value> </Init-param> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Dispatcherservlet</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping> <!--set character encoding via filter - <Filter> <Filter-name>Characterencodingfilter</Filter-name> <Filter-class>Org.springframework.web.filter.CharacterEncodingFilter</Filter-class> <Init-param> <Param-name>Encoding</Param-name> <Param-value>Utf-8</Param-value> </Init-param> </Filter> <filter-mapping> <Filter-name>Characterencodingfilter</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping> </Web-app>
3, Configuration Applicationcontext.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.2.xsd "> <Context:component-scanBase-package= "Com.action"/> <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "prefix"value="/" /> < Propertyname= "suffix"value= ". jsp" /> </Bean> </Beans>
4. Create controller--->> handle requests
Packagecom.action;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;ImportCom.entity.User; @Controller Public classuseraction {@RequestMapping ("Submit") PublicModelandview Show (user user) {Modelandview m=NewModelandview ("list"); M.addobject ("Uname", User.getuname ()); M.addobject ("UPass", User.getupass ()); returnm; }}
Ii. several ways of parameter transfer in controller controllers
1, can be injected through the JavaBean already created
Import Com.entity.User; @Controller Public class useraction { @RequestMapping ("Submit") public modelandview Show (user user) { New Modelandview ("list"); M.addobject ("uname", User.getuname ()); M.addobject ("UPass", User.getupass ()); return m; }}
2, the corresponding name auto-fill
@RequestMapping ("Submit") public modelandview Show (String uname,string UPass, HttpServletRequest request) { new modelandview ("list"); M.addobject ("uname", uname); // need to match the name of the foreground M.addobject ("UPass", UPass); // need to match the name of the foreground return m; }
3. If the name is inconsistent, you need to add an annotation description
@RequestMapping ("Submit" /** * If the name of the foreground name does not match the parameter name, use the * value parameter name * Required whether it must be * defaultvalue if it is empty, the current Default value */ public modelandview Show (@Re Questparam (value= "uname", Required=false , defaultvalue= "xxx" ) String uname,string UPass) {modelandview m = new Model Andview ("list" "uname", uname); // needs to be consistent with the name of the foreground m.addobject ("UPass", UPass); // return m; }
Spring MVC Architecture (v)