首页 > 学院 > 开发设计 > 正文

SpringMVC 学习 03 - spring mvc的获取参数和传递参数

2019-11-08 20:23:05
字体:
来源:转载
供稿:网友

工程结构

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

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

[html] view plain copy<servlet>      <servlet-name>hello</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param><!-- 这两行是重新定位spring的配置文件路径,默认是在WEB-INF下面的hello-servlet.xml -->          <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,书写spring的配置文件

[html] view plain copy<?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">            <!-- 开始spring mvc的注解 -->      <mvc:annotation-driven/>            <!-- 这样根目录下面的resource的文件(.CSS,.js等)就不会被spring的DispatchServlet进行过滤 -->      <mvc:resources location="/resources/" mapping="/resources/**"/>            <!-- 配置注解扫描的包路径 -->      <context:component-scan base-package="cn.edu.hj.controller"></context:component-scan>            <!-- 配置action中返回的视图配置  -->      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">          <property name="prefix" value="/WEB-INF/jsp/"></property>          <property name="suffix" value=".jsp"></property>      </bean>            <!-- 上传文件时需要做的配置 -->      <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">          <property name="maxUploadSize" value="5000000"></property>      </bean>        </beans>  

4,书写action中的代码

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

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

[java] view plain copypackage 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")//表示要访问这个action的时候都要加上这个/hello路径  public class HelloController {        /* 接收参数getParameter()的时候:  * 如果地址栏/springmvc/hello.htm上面没有传递参数,那么当id为int型的时候会报错,当id为Integer的时候值为null  * 当地址栏为/springmvc/hello.htm?id=10的时候,action中有三种接收方式  * 1、String hello(@RequestParam(value = "userid") int id),这样会把地址栏参数名为userid的值赋给参数id,如果用地址栏上的参数名为id,则接收不到  * 2、String hello(@RequestParam int id),这种情况下默认会把id作为参数名来进行接收赋值  * 3、String hello(int id),这种情况下也会默认把id作为参数名来进行接收赋值  * 注:如果参数前面加上@RequestParam注解,如果地址栏上面没有加上该注解的参数,例如:id,那么会报404错误,找不到该路径  */      @RequestMapping(value = "/hello.htm")      public String hello(int id){//getParameter()的方式          System.out.println("hello action:"+id);  //      return "hello";          return "redirect:/index.jsp";//不能重定向web-info里面的文件,而且需要写上绝对路径      }            //返回页面参数的第一种方式,在形参中放入一个map      @RequestMapping(value = "/hello1.htm")      public String hello(int id,Map<String,Object> map){          System.out.println("hello1 action:"+id);          map.put("name", "huangjie");          return "hello";      }            //返回页面参数的第二种方式,在形参中放入一个Model      @RequestMapping(value = "/hello2.htm")      public String hello2(int id,Model model){          System.out.println("hello2 action:"+id);          model.addAttribute("name", "huangjie");          //这个只有值没有键的情况下,使用Object的类型作为key,String-->string          model.addAttribute("ok");          return "hello";      }        //得到request,response,session等,只要在方法形参中声明参数即可      @RequestMapping(value = "/hello3.htm")      public String hello3(HttpServletRequest request){          String id = request.getParameter("id");          System.out.println("hello3 action:"+id);          return "hello";      }  }  5,书写返回视图的jsp页面

/WebRoot下面的index.jsp

[html] view plain copy<body>     This is my JSP page. <br>  </body>  /WebRoot/WEB-INF下面的hello.jsp

[html] view plain copy<body>    This is my hello page. <br>    name:${name } <br>    value:${string} <br>  </body>  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表