码迷,mamicode.com
首页 > 编程语言 > 详细

(五)springmvc之获取表单提交的数据

时间:2017-11-02 18:09:30      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:actor   java   checkbox   注解   用户名   autowire   ping   cep   http   

8.1:使用Request
    <form method="post" id="form1" action="<%=request.getContextPath()%>/formData/formData_1">
        <input type="text" name="username" value="用户名"/>
        <input type="checkbox" name="check_1" value="复选框1"/>复选框1
        <input type="checkbox" name="check_1" value="复选框2"/>复选框2
        <input type="checkbox" name="check_1" value="复选框3"/>复选框3
        <input type="submit" value="提交"/>
    </form>
    
    8.2:使用形参注解
    <form method="post" id="form1" action="<%=request.getContextPath()%>/formData/formData_2">
        <input type="text" name="username" value="用户名"/>
        <input type="checkbox" name="check_1" value="复选框1"/>复选框1
        <input type="checkbox" name="check_1" value="复选框2"/>复选框2
        <input type="checkbox" name="check_1" value="复选框3"/>复选框3
        <input type="submit" value="提交"/>
    </form>    
    8.3:使用对象来获取
    <form method="post" id="form1" action="<%=request.getContextPath()%>/formData/formData_3">
        <input type="text" name="username" value="用户名"/>
        <input type="checkbox" name="check_1" value="复选框1"/>复选框1
        <input type="checkbox" name="check_1" value="复选框2"/>复选框2
        <input type="checkbox" name="check_1" value="复选框3"/>复选框3
        <input type="submit" value="提交"/>
    </form>    

controller

package com.controller.formdata;

import java.util.Arrays;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bean.UserBean;

@Controller
@RequestMapping(value = "/formData")
public class FormData {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private HttpSession session;
    @Autowired
    private ServletContext servletContext;

    
    @RequestMapping(value = "/formData_1")
    public ModelAndView formData_1() throws Exception {
        this.request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String[] check_1 = request.getParameterValues("check_1");

        System.out.println(username);
        System.out.println(Arrays.asList(check_1));

        return null;
    }

    @RequestMapping(value = "/formData_2")
    public ModelAndView formData_2(
            @RequestParam(name = "username") String username_2, String[] check_1)
            throws Exception {
        System.out.println("formData_2方法");
        System.out.println(username_2);
        System.out.println(Arrays.asList(check_1));

        return null;
    }

    /**
     * 类似Struts中的模型驱动
     * 
     * @param userBean
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/formData_3")
    public ModelAndView formData_3(UserBean userBean) throws Exception {
        System.out.println("formData_3方法");
        System.out.println(userBean.getUsername());
        System.out.println(Arrays.asList(userBean.getCheck_1()));

        return null;
    }

}
UserBean.java
public class UserBean implements Serializable {
    private String username;
    private String[] check_1;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String[] getCheck_1() {
        return check_1;
    }

    public void setCheck_1(String[] check_1) {
        this.check_1 = check_1;
    }

}

 












(五)springmvc之获取表单提交的数据

标签:actor   java   checkbox   注解   用户名   autowire   ping   cep   http   

原文地址:http://www.cnblogs.com/shyroke/p/7773288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!