动力节点旗下在线教育品牌  |  咨询热线:400-8080-105 学Java全栈,上蛙课网
首页 > 文章

Spring MVC封装表单数据源码

08-27 17:22 57浏览
举报 T字号
  • 大字
  • 中字
  • 小字

Spring MVC作为目前主流的开发框架之一,其覆盖了庞杂的知识体系,我们之前已经介绍了很多关于Spring MVC的知识,但是还是有很多容易被我们平时忽视的知识点,比如说本文要讲的Spring MVC封装表单数据源码
    我们先来看看什么是数据封装(Data Encapsulation),笼统地讲,就是把业务数据映射到某个封装协议的净荷中,然后填充对应协议的包头,形成封装协议的数据包,并完成速率适配。所以,这一过程在Spring MVC框架开发过程中也是必不可少的,下面我们来看Spring MVC封装表单数据源码。

1.domain类

 package com.xiaostudy.domain;

 

 public class User {

    private int id;

    private String username;

     private String password;

     public int getId() {

         return id;

     }

     public void setId(int id) {

         this.id = id;

     }

 

    public String getUsername() {

         return username;

     }

 

     public void setUsername(String username) {

         this.username = username;

    }

 

     public String getPassword() {

        return password;

     }

     public void setPassword(String password) {

         this.password = password;

     }

     @Override

    public String toString() {

         return "User [id=" + id + ", username=" + username + ", password=" + password + "]";

     }

 

 }

2.填写表单数据的JSP

<%@ 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">
 <html>
 <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>springMVC</title>
 </head>
 <body>
 <form action="${pageContext.request.contextPath }/command.do" method="post">
     ID:<input type="text" name="id" id="id"/>
     姓名:<input type="text" name="username" id="username"/>
    密码:<input type="password" name="password" id="password"/>
     <input type="submit" value="提交"/>
 </form>
 </body>
 </html>

3.springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
                        http://www.springframework.org/schema/mvc 
                         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
                         http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
                         http://www.springframework.org/schema/tx 
                         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
        
         <!-- ====================== -->
        <!-- 配置处理器映射器,springmvc默认的处理器映射器
        BeanNameUrlHandlerMapping:根据bean(自定义Controler)的name属性的url去寻找hanler(Action:Controller) -->
     <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>         
    <bean name="/toadd.do" class="com.xiaostudy.ToAddController"/>
    <bean name="/command.do" class="com.xiaostudy.CommandController"/>
         <!-- 配置sprigmvc视图解析器:解析逻辑试图 
             后台返回逻辑试图:index
            视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/index.jsp -->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/"/>
            <property name="suffix" value=".jsp"/>        
         </bean>
</beans>

4.负责跳转到add.jsp的类

package com.xiaostudy;

 

 import javax.servlet.http.HttpServletRequest;

 import javax.servlet.http.HttpServletResponse;

 

 import org.springframework.web.servlet.ModelAndView;

 import org.springframework.web.servlet.mvc.Controller;

 

 public class ToAddController implements Controller{

 

     public ModelAndView handleRequest(HttpServletRequest request,

            HttpServletResponse response) throws Exception {

         ModelAndView mv = new ModelAndView();

         //调转到add添加页面视图

         mv.setViewName("add");

         return mv;

     }

}

5.处理表单数据类

  package com.xiaostudy;

 

 import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 import org.springframework.validation.BindException;

 import org.springframework.web.servlet.ModelAndView;

 import org.springframework.web.servlet.mvc.AbstractCommandController;

 import com.xiaostudy.domain.User;

 public class CommandController extends AbstractCommandController {

     

    public CommandController() {

        this.setCommandClass(User.class);

    }

    @Override

    protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException arg3)

             throws Exception {

         User user = (User)command;

        ModelAndView mv = new ModelAndView();

        mv.addObject("user", user);

        mv.setViewName("index");

       return mv;

    }

 }

6.返回到jsp中

<%@ 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">
 <html>
 <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>springMVC_demo</title>
</head>
 <body>
     ${user.id }||${user.username }||${user.password }
 </body>
 </html>

上述的六个部分是Spring MVC封装表单数据的核心源码,缺一不可。当然,根据不同的开发环境和配置需要适当的调整一些代码,具体可以参考本站的Spring MVC视频教程,里面有整个Spring MVC框架的源码。

 

 

 

 

0人推荐
共同学习,写下你的评论
0条评论
袋子里的小女孩
程序员袋子里的小女孩

12篇文章贡献56226字

作者相关文章更多>

推荐相关文章更多>

DOM渲染的详细过程

QCode09-04 14:38

CSS水平和垂直居中技巧大梳理

Code大师09-04 14:50

mui的input框在IOS系统下无法聚焦或点击多次才能聚焦

不写代码你养我啊08-23 11:14

推荐的-视.频播放器以及在线客服

不写代码你养我啊09-17 18:02

谈谈java多线程的三大特性

要学习了06-18 18:13

发评论

举报

0/150

取消