现在的位置: 首页 > 综合 > 正文

spring mvc的获取参数和传递参数

2018年08月17日 ⁄ 综合 ⁄ 共 4673字 ⁄ 字号 评论关闭

1、工程结构

在lib下面加入jar包,jar图片如下所示:

2,配置web.xml,也就是spring mvc设置启动的servlet

[html] view
plain
copy

  1. <servlet>  
  2.     <servlet-name>hello</servlet-name>  
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.     <init-param><!-- 这两行是重新定位spring的配置文件路径,默认是在WEB-INF下面的hello-servlet.xml -->  
  5.         <param-name>contextConfigLocation</param-name>  
  6.         <param-value>WEB-INF/web-main.xml</param-value>  
  7.     </init-param>  
  8. </servlet>  
  9. <servlet-mapping>  
  10.     <servlet-name>hello</servlet-name>  
  11.     <url-pattern>*.htm</url-pattern>  
  12. </servlet-mapping>  


3,书写spring的配置文件

[html] view
plain
copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  9.       
  10.     <!-- 开始spring mvc的注解 -->  
  11.     <mvc:annotation-driven/>  
  12.       
  13.     <!-- 这样根目录下面的resource的文件(.css,.js等)就不会被spring的DispatchServlet进行过滤 -->  
  14.     <mvc:resources location="/resources/" mapping="/resources/**"/>  
  15.       
  16.     <!-- 配置注解扫描的包路径 -->  
  17.     <context:component-scan base-package="cn.edu.hj.controller"></context:component-scan>  
  18.       
  19.     <!-- 配置action中返回的视图配置  -->  
  20.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  21.         <property name="prefix" value="/WEB-INF/jsp/"></property>  
  22.         <property name="suffix" value=".jsp"></property>  
  23.     </bean>  
  24.       
  25.     <!-- 上传文件时需要做的配置 -->  
  26.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  27.         <property name="maxUploadSize" value="5000000"></property>  
  28.     </bean>  
  29.       
  30. </beans>  


4,书写action中的代码

int userId = ServletRequestUtils.getIntParameter(request, "userId",-1);

ServletRequestUtils是Spring 2.0新增的工具类,可以方便地按类型获取请求参数的值,它位于org.springframework.web.bind包中。

[java] view
plain
copy

  1. package cn.edu.hj.controller;  
  2. import java.util.Map;  
  3. import javax.servlet.http.HttpServletRequest;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.ui.Model;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestParam;  
  8.   
  9. @Controller  
  10. //@RequestMapping(value = "/hello")//表示要访问这个action的时候都要加上这个/hello路径  
  11. public class HelloController {  
  12.       
  13. /* 接收参数getParameter()的时候: 
  14.  * 如果地址栏/springmvc/hello.htm上面没有传递参数,那么当id为int型的时候会报错,当id为Integer的时候值为null 
  15.  * 当地址栏为/springmvc/hello.htm?id=10的时候,action中有三种接收方式 
  16.  * 1、String hello(@RequestParam(value = "userid") int id),这样会把地址栏参数名为userid的值赋给参数id,如果用地址栏上的参数名为id,则接收不到 
  17.  * 2、String hello(@RequestParam int id),这种情况下默认会把id作为参数名来进行接收赋值 
  18.  * 3、String hello(int id),这种情况下也会默认把id作为参数名来进行接收赋值 
  19.  * 注:如果参数前面加上@RequestParam注解,如果地址栏上面没有加上该注解的参数,例如:id,那么会报404错误,找不到该路径 
  20.  */  
  21.     @RequestMapping(value = "/hello.htm")  
  22.     public String hello(int id){//getParameter()的方式  
  23.         System.out.println("hello action:"+id);  
  24. //      return "hello";  
  25.         return "redirect:/index.jsp";//不能重定向web-info里面的文件,而且需要写上绝对路径  
  26.     }  
  27.       
  28.     //返回页面参数的第一种方式,在形参中放入一个map  
  29.     @RequestMapping(value = "/hello1.htm")  
  30.     public String hello(int id,Map<String,Object> map){  
  31.         System.out.println("hello1 action:"+id);  
  32.         map.put("name""huangjie");  
  33.         return "hello";  
  34.     }  
  35.       
  36.     //返回页面参数的第二种方式,在形参中放入一个Model  
  37.     @RequestMapping(value = "/hello2.htm")  
  38.     public String hello2(int id,Model model){  
  39.         System.out.println("hello2 action:"+id);  
  40.         model.addAttribute("name""huangjie");  
  41.         //这个只有值没有键的情况下,使用Object的类型作为key,String-->string  
  42.         model.addAttribute("ok");  
  43.         return "hello";  
  44.     }  
  45.   
  46.     //得到request,response,session等,只要在方法形参中声明参数即可  
  47.     @RequestMapping(value = "/hello3.htm")  
  48.     public String hello3(HttpServletRequest request){  
  49.         String id = request.getParameter("id");  
  50.         System.out.println("hello3 action:"+id);  
  51.         return "hello";  
  52.     }  
  53. }  


5,书写返回视图的jsp页面

/WebRoot下面的index.jsp

[html] view
plain
copy

  1. <body>  
  2.    This is my JSP page. <br>  
  3. </body>  


/WebRoot/WEB-INF下面的hello.jsp

[html] view
plain
copy

  1. <body>  
  2.   This is my hello page. <br>  
  3.   name:${name } <br>  
  4.   value:${string} <br>  
  5. </body>  


整个工程的下载地址为:http://download.csdn.net/detail/wxwzy738/5224307

转载于:http://blog.csdn.net/wxwzy738/article/details/8762773

抱歉!评论已关闭.