8.1: Use request<formMethod= "POST"ID= "Form1"Action= "<%=request.getcontextpath ()%>/formdata/formdata_1"> <inputtype= "text"name= "username"value= "User name"/> <inputtype= "checkbox"name= "Check_1"value= "check box 1"/>check box 1<inputtype= "checkbox"name= "Check_1"value= "check box 2"/>check box 2<inputtype= "checkbox"name= "Check_1"value= "check box 3"/>check box 3<inputtype= "Submit"value= "Submit"/> </form>8.2: Using formal parameter Annotations<formMethod= "POST"ID= "Form1"Action= "<%=request.getcontextpath ()%>/formdata/formdata_2"> <inputtype= "text"name= "username"value= "User name"/> <inputtype= "checkbox"name= "Check_1"value= "check box 1"/>check box 1<inputtype= "checkbox"name= "Check_1"value= "check box 2"/>check box 2<inputtype= "checkbox"name= "Check_1"value= "check box 3"/>check box 3<inputtype= "Submit"value= "Submit"/> </form>8.3: Use objects to get<formMethod= "POST"ID= "Form1"Action= "<%=request.getcontextpath ()%>/formdata/formdata_3"> <inputtype= "text"name= "username"value= "User name"/> <inputtype= "checkbox"name= "Check_1"value= "check box 1"/>check box 1<inputtype= "checkbox"name= "Check_1"value= "check box 2"/>check box 2<inputtype= "checkbox"name= "Check_1"value= "check box 3"/>check box 3<inputtype= "Submit"value= "Submit"/> </form>
Controller
PackageCom.controller.formdata;Importjava.util.Arrays;ImportJavax.servlet.ServletContext;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.servlet.ModelAndView;ImportCom.bean.UserBean; @Controller @requestmapping (Value= "/formdata") Public classFormData {@AutowiredPrivateHttpServletRequest request; @AutowiredPrivatehttpservletresponse response; @AutowiredPrivateHttpSession session; @AutowiredPrivateServletContext ServletContext; @RequestMapping (Value= "/formdata_1") PublicModelandview formdata_1 ()throwsException { 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") PublicModelandview formdata_2 (@RequestParam (name= "username") String username_2, string[] check_1)throwsException {System.out.println ("Formdata_2 Method"); System.out.println (username_2); System.out.println (Arrays.aslist (check_1)); return NULL; } /*** Similar to model-driven in struts * *@paramUserBean *@return * @throwsException*/@RequestMapping (Value= "/formdata_3") PublicModelandview formdata_3 (UserBean UserBean)throwsException {System.out.println ("Formdata_3 Method"); System.out.println (Userbean.getusername ()); System.out.println (Arrays.aslist (Userbean.getcheck_1 ())); return NULL; }}
Userbean.java
Public classUserBeanImplementsSerializable {PrivateString username; Privatestring[] Check_1; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } Publicstring[] Getcheck_1 () {returnCheck_1; } Public voidsetcheck_1 (string[] check_1) { This. Check_1 =Check_1; }}
(v) Springmvc to obtain the data submitted by the form