《SpringMvc》----请求参数的绑定1

来源:互联网 发布:知乎阿里dva框架react 编辑:程序博客网 时间:2024/05/22 06:44

前言

  • SpringMvc是Spring的一个模块,现在在企业中的应用已经超过Struts2,顾名思义,SpringMvc也遵循一种MVC设计模式,当页面与后台交互的时候,用到了参数的绑定,下面小编利用项目中的经验,简单的总结一下SpringMvc中请求参数的绑定。

一、SpringMvc开发步骤

引入必要的jar包

  • 此处省略…….

配置前端控制器

<!-- 前端控制器 --><servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- contextConfigLocation指定 springmvc的全局配置文件 如果 contextConfigLocation不指定,默认找配置文件名称:servlet的name+"-servlet.xml" -->    <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:springmvc.xml</param-value>    </init-param></servlet><servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>*.action</url-pattern></servlet-mapping>

配置三大组件

三大组件分别是处理器映射器、处理器适配器、视图解析器。

    <!-- 组件扫描 只扫描action -->    <context:component-scan base-package="cn.dmsd.springmvc.action" />    <!-- 使用<mvc:annotation-driven />替换上边定义的处理器映射器和适配器 -->    <mvc:annotation-driven />    <!-- 视图解析器 解析jsp视图,默认使用jstl,要求classpath下有jstl的jar包 -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 视图的前缀 -->        <property name="prefix" value="/WEB-INF/jsp/" />        <!-- 视图的后缀 -->        <property name="suffix" value=".jsp" />    </bean></beans>

编写Handler(参数绑定)

1、默认支持的参数类型

  1. HttpServletRequest:通过request对象获取请求信息。
  2. HttpServletResponse:通过response处理响应信息。
  3. HttpSession:通过session对象得到session中存放的对象.
  4. Model:通过model向页面传递数据。

Handler代码

@RequestMapping(value="/querystudent")    public String querystudent(HttpSession session,HttpServletRequest request,HttpServletResponse response,Model model) throws Exception {        //将学生信息列表在页面上展示        List<Student> list = new ArrayList<Student>();        Student student = new Student();        student.setId("s001");        student.setName("小学生");        student.setAge(32);        student.setBirthday(new Date());        list.add(student);        //利用model向页面传递数据            model.addAttribute("list", list);        //向session存入信息,供全局使用        session.setAttribute("name", "美女");        //通过request获取传递的参数        String userName=request.getParameter("userName");        return "student/querystudent";    }

Jsp代码

<form action="deletestu.action" method="post">你好:${sessionScope.name}<!-- <input type="submit" value="批量删除"/> --><table width="100%" border=1>  <tr>    <td>选择</td>    <td>姓名 </td>    <td>年龄</td>    <td>出生日期</td>    <td>操作</td>  </tr> <c:forEach items="${list}" var="stu">    <tr>     <td><input type="checkbox" name="deleteid" value="${stu.id }"/></td>    <td>${stu.name } </td>    <td>${stu.age } </td>    <td><fmt:formatDate value="${stu.birthday }" pattern="yyyy-MM-dd"/> </td>    <td><a href="editstudent/${stu.id}.action">修改</a></td>  </tr> </c:forEach></table></form>

效果图如下所示

这里写图片描述

2、表单对象-pojo

jsp代码

<form action="${pageContext.request.contextPath }/stu/editstudentsubmit.action" method="post">    <input type="hidden" name="id_" value="${student.id}"/>    姓名:<input name="name" value="${student.name}"/>    <br/>    年龄:<input name="age" value="${student.age}"/>    <br/>    出生日期:<input name="birthday" value="<fmt:formatDate value="${student.birthday}" pattern="yyyy-MM-dd"/>"/>    <br/>    用户状态:    <input type="radio" name="student.userstate" value="true"/>正常    <input type="radio" name="student.userstate" value="false"/>暂停    <br/>    课程成绩:    <input type="submit" value="提交 "/> </form>

Handelr代码(向Handler请求数据)

@RequestMapping(value="/editstudent",method=RequestMethod.GET)public String editstudent(Model model) throws Exception{    Student student =new Student();    student.setName("张三");    student.setId("222");    student.setAge(22);    student.setBirthday(new Date());    model.addAttribute("student",student);    return "student/editstudent";}

向Handler发送数据

@RequestMapping("/editstudentsubmit")    public String editstudentsubmit(Student student) throws Exception{        System.out.println(student);        return "success";    }

小结:

  • 上面是SpringMvc简单开发的小步骤和一些简单类型的参数绑定,下一篇博客小编介绍字符数组绑定与List绑定。
1 0