<!--the name of the front and rear table is the same, receive the parameter-->
3. What happens when the foreground data type and background receive data types are inconsistent under test?
@RequestMapping (value= "par3.do")//test can not set the parameter data type public Modelandview par3 (Integer uname,string upass) { SYSTEM.OUT.PRINTLN (uname+ "parameter data type" +upass); Modelandview model = new Modelandview ("Para"), Model.addobject ("Para", "parameter data type"), return model;
Front desk data
<!--receive parameter data type-->
Result: If the data type of the front and back table is inconsistent, the error
4. Pass the parameter of the object
@RequestMapping (value= "par4.do")//Pass the value of the object public Modelandview PAR4 (user user) {System.out.println (user.tostring () + " Pass the object to the parameter "); Modelandview model = new Modelandview ("Para"), Model.addobject ("Para", "Pass through Object"), return model;
corresponding front desk
<!--the name names must be consistent with the object property names through the object parameter, note that the parameters data type is also consistent-->
See here a time, backstage need us to deal with the time
Time attribute editor @initbinderpublic void Initbinder (Servletrequestdatabinder bin) {//time receive format, can be defined according to your own requirements SimpleDateFormat SDF = New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Customdateeditor cust = new Customdateeditor (SDF, True); Bin.registercustomeditor (Date.class, Cust);}
Take a look at the full code
Directory structure
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/95/46/wKioL1kTwLPjyxogAABPrWAy8eo380.png-wh_500x0-wm_ 3-wmp_4-s_646558588.png "title=" 111.png "alt=" Wkiol1ktwlpjyxogaabprway8eo380.png-wh_50 "/>
Testparameter Complete code
package com.control;import java.text.simpledateformat;import java.util.date;import javax.servlet.http.httpservletrequest;import org.springframework.beans.propertyeditors.customdateeditor; import org.springframework.stereotype.controller;import org.springframework.web.bind.servletrequestdatabinder;import org.springframework.web.bind.annotation.initbinder;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.servlet.modelandview;import com.entity.user; @Controller @requestmapping ("/user/") public class testparameter {@RequestMapping (value= "par1.do") public modelandview par1 ( Httpservletrequest request) {// receive the reception of the parameters String username = request.getparameter (" UserName "); String password = request.getparameter ("PassWord");// console print input The value of the parameter System.out.println (username+ "===httpservletrequest=====" +password);// view Jump Modelandview mOdel = new modelandview ("Para"); Model.addobject ("Para", "HttpServletRequest");return Model;} @RequestMapping (value= "par2.do")// uname and uPass are the parameters of the reception, the name is consistent, there is no pre-order requirements public Modelandview par2 (String uname,string upass) {System.out.println (uname+ "same name as" +upass "); Modelandview model = new modelandview ("Para"); Model.addobject ("Para", "the same name as before and after"); Return model;} @RequestMapping (value= "par3.do")// test can not set the parameter data type// after testing, if the data type from the foreground and receive the definition of data type inconsistent, will be error public MODELANDVIEW&NBSP;PAR3 (Integer uname,string upass) {System.out.println (uname+ "parameter data type" +upass); Modelandview model = new modelandview ("Para"); Model.addobject ("Para", "parameter data type"); Return model;} @RequestMapping (value= "par4.do")// pass the value of the object Public modelandview par4 (user user) { System.out.println (user.tostring () + "pass-through object"); Modelandview model = new modelandview ("Para"); MODEL.ADdobject ("Para", "pass Object"); Return model;} Time attribute editor, which can be extracted here as public method @initbinderpublic void initbinder (servletrequestdatabinder bin) {// Time receive Format Simpledateformat sdf = new simpledateformat ("Yyyy-mm-dd hh:mm:ss"); Customdateeditor cust = new customdateeditor (sdf, true); Bin.registerCustomEditor ( date.class, cust);}} 3.index.jsp full Code
<%@ page language= "java" contenttype= "Text/html; charset=utf-8" pageencoding= "UTF-8"%><! doctype html public "-//w3c//dtd html 4.01 transitional//en" "HTTP// Www.w3.org/TR/html4/loose.dtd ">4.para.jsp Code
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
5.web.xml Code
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "version=" 2.5 "><servlet><servlet-name>springmvc</ Servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup></servlet><!--Map All requests to the Dispatcherservlet for Handling--><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do </url-pattern></servlet-mapping></web-app>
6.springmvc-servlet.xml Code
<?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 "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xsi:schemalocation = "http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans /spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-3.1.xsd "><!-- Note drivers --><mvc:annotation-driven/><!-- Annotation scan --><context:component-scan base-package= "Com.control"/><!-- View Resolution --> <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ><property Name= "prefix" value= "/view/" ></property><properTy name= "suffix" value= ". JSP" ></property></bean></beans>
This article is from the "11134439" blog, please be sure to keep this source http://11144439.blog.51cto.com/11134439/1924370
SPRINGMVC parameter and receive parameters