• (五)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;
        }
    
    }











  • 相关阅读:
    笔记:Struts2 的 JSON 插件
    笔记:Struts2 拦截器
    笔记:Struts2 文件上传和下载
    笔记:Struts2 文件上传和下载
    【学习总结】推荐系统-协同过滤原理
    【刷题】牛客网看到的鹅厂ML面筋-部分问题RecSys相关
    【刷题】【LeetCode】000-十大经典排序算法
    【刷题】【LeetCode】总
    【问题解决方案】pygame生成的窗口点右上角关闭按钮未响应问题的解决
    【刷题】若串 =’software’ ,其子串数目为:37
  • 原文地址:https://www.cnblogs.com/shyroke/p/7773288.html
Copyright © 2020-2023  润新知