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

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;
    }

}

 












posted @ 2017-11-02 17:21  shyroke、  阅读(608)  评论(0编辑  收藏  举报
作者:shyroke 博客地址:http://www.cnblogs.com/shyroke/ 转载注明来源~