One, spring MVC get parameters and pass parameters

Source: Internet
Author: User
Tags bind xmlns

1. Engineering structure


Add the jar package below Lib, and the jar picture looks like this:


2, configure Web. XML, which is the servlet that spring MVC sets up to start

  <servlet>
  	<servlet-name>hello</servlet-name>
  	<servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>
  	<init-param><!-- These two lines are repositioning spring's profile path, which by default is under Web-inf hello-servlet.xml--and
  		<param-name>contextconfiglocation</ param-name>
  		<param-value>WEB-INF/web-main.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>hello</servlet-name>
  	< Url-pattern>*.htm</url-pattern>
  </servlet-mapping>

3. Write the Spring configuration file

<?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.xsd Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!--start with spring MVC annotations-&LT;MVC: Annotation-driven/> <!--Such a root directory resource files (. css,.js, etc.) will not be filtered by spring's dispatchservlet--&LT;MVC: Resources location= "/resources/" mapping= "/resources/**"/> <!--configuration Annotation Scanned package path--<context:component-scan BA Se-package= "Cn.edu.hj.controller" ></context:component-scan> <!--Configure actionThe view returned in the configuration--<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > < Property name= "prefix" value= "/web-inf/jsp/" ></property> <property name= "suffix" value= ". JSP" ></ Property> </bean> <!--you need to do when uploading files--<bean id= "Multipartresolver" class= "Org.springframework.web. Multipart.commons.CommonsMultipartResolver "> <property name=" maxuploadsize "value=" 5000000 "></ Property> </bean> </beans>

4. Write the code in the action

int userId = Servletrequestutils.getintparameter (Request, "UserId",-1);

Servletrequestutils is a new tool class in spring 2.0 that makes it easy to get the value of the request parameter by type, which is located in the Org.springframework.web.bind package.

Package Cn.edu.hj.controller;
Import Java.util.Map;
Import Javax.servlet.http.HttpServletRequest;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.Model;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RequestParam; @Controller//@RequestMapping (value = "/hello")//indicates that this action is to be accessed by adding this/hello path public class Hellocontroller {/* receive parameter G Etparameter () When: * If the address bar/springmvc/hello.htm above does not pass parameters, then when the ID of the int type will be error, when the ID is an integer value is null * When the address bar is/springmvc/ HELLO.HTM?ID=10, there are three ways to receive * 1, String hello (@RequestParam (value = "userid") int id) in action, which assigns the value of the address bar parameter named UserID to the parameter ID. If you use the parameter name on the address bar as the ID, you will not receive the * 2, string hello (@RequestParam int id), in which case the ID as the parameter name to receive assignment * 3, string hello (int id), in this case will also default ID as parameter name to receive assignment * Note: If the parameter is preceded by a @requestparam annotation, if the address bar does not include the annotation parameter, for example: ID, then the 404 error is reported, the path is not found */@RequestMapping (value = "/ Hello.htm ") Public String hello (int id) {//getparameter () way System.out.println (" Hello Action: "+id);
return "Hello"; return "redirect:/index.jsp";//cannot redirect Web-info inside file, and need to write on absolute path}//Return the first method of the page parameter, put a map @RequestMapping in the formal parameter (value = "/
		Hello1.htm ") Public String hello (int id,map<string,object> Map) {System.out.println (" Hello1 Action: "+id);
		Map.put ("name", "Huangjie");
	return "Hello"; }//Return the second way of the page parameter, put a Model @RequestMapping in the formal parameter (value = "/hello2.htm") public String hello2 (int id,model Model) {Sys
		Tem.out.println ("Hello2 Action:" +id);
		Model.addattribute ("name", "Huangjie");
		In the case of a value without a key, use the type of object as Key,string-->string model.addattribute ("OK");
	return "Hello"; }//Get request,response,session, etc., as long as you declare the parameter in the method parameter @RequestMapping (value = "/hello3.htm") public String Hello3 (httpservle
		Trequest request) {String id = request.getparameter ("id");
		System.out.println ("Hello3 Action:" +id);
	return "Hello"; }
}

5. Write a JSP page that returns a view

/webroot below the index.jsp

<body> This is
    my JSP page. <br>
 </body>

/webroot/web-inf below the hello.jsp

  <body> This is
    my Hello page. <br>
    name:${name} <br>
    value:${string} <br>
  </body>

The download address for the entire project is: http://download.csdn.net/detail/wxwzy738/5224307

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.