1. Configure spring mvc
1. Add the following configuration in the web. xml file:
<servlet> <servlet-name>MVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-config.xml</param-value> </init-param></servlet><servlet-mapping> <servlet-name>MVC</servlet-name> <url-pattern>*.do</url-pattern></servlet-mapping>
2. mvc-config.xml file configuration
<? 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: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans -3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! -- Automatically scans the package name --> <context: component-scan base-package = "cn. ecgonline. eis. controller."/> <! -- Support for default annotation ing --> <mvc: annotation-driven/> <! -- Start the annotation function of Spring MVC to map requests and POJO annotations <bean class = "org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerAdapter"/>><! -- View interpretation class --> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/jsp/"/> <property name =" suffix "value = ". jsp "/> <! -- Can be empty to facilitate the selection of view interpretation class Logic Based on the extension --> <property name = "viewClass" value = "org. springframework. web. servlet. view. jstlView "/> </bean> </beans>
2. Create a controller class to obtain the foreground property value
I use the annotation method.
1. Obtain a single attribute using the common data type in the background
Controller class:
Package cn. ecgonline. eis. controller. workstation; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. modelAttribute; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. servlet. modelAndView;/*** test * @ author Chen Wenlong * @ date 12:49:53 */@ Controllerpublic class WrokstationController {@ RequestMapping ("/new_works Tation. do ") public ModelAndView newWorkStation (@ RequestParam Stringusername) {System. out. println (user. getUsername); ModelAndView mv = new ModelAndView ("Hello"); mv. addObject ("test", "hello spring mvc! "); Return mv ;}}
@ RequestParam annotation indicates a single attribute
Jsp page code:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Ii. Background useModel classGet Data
Model class:
package test;public class User{ private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
Controller class:
Package cn. ecgonline. eis. controller. workstation; import javax. annotation. resource; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. modelAttribute; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. servlet. modelAndView;/*** test * @ author Chen Wenlong * @ date 12:49:53 */@ Controllerpublic class WrokstationContro Roller {@ RequestMapping ("/new_workstation.do") public ModelAndView newWorkStation (@ ModelAttribute User user) {System. out. println (user. getUsername); System. out. println (user. getPassword); ModelAndView mv = new ModelAndView ("Hello"); mv. addObject ("test", "hello spring mvc! "); Return mv ;}}
@ ModelAttributeAnnotations indicate that the model is used to receive values. The attributes in the User object must correspond to the attributes on the jsp page.
Jsp page code:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">