spring mvc:復選框(多選)


以user為例,user下有 username用戶,password密碼, address地址, receivePaper是否訂閱, favotireFramework興趣愛好,

user.java

public class User {

   private String username;
   private String password;
   private String address;
   private boolean receivePaper;
   private String [] favoriteFrameworks;   

   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;
   }
   public String getAddress() {
      return address;
   }
   public void setAddress(String address) {
      this.address = address;
   }
   public boolean isReceivePaper() {
      return receivePaper;
   }
   public void setReceivePaper(boolean receivePaper) {
      this.receivePaper = receivePaper;
   }
   public String[] getFavoriteFrameworks() {
      return favoriteFrameworks;
   }
   public void setFavoriteFrameworks(String[] favoriteFrameworks) {
      this.favoriteFrameworks = favoriteFrameworks;
   }
}

  userController.java

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;

@Controller
public class UserController {

   @RequestMapping(value = "/user", method = RequestMethod.GET)
   public ModelAndView user() {
      User user = new User();      
      user.setFavoriteFrameworks((new String []{"Spring MVC","Struts 2"}));
      user.setGender("M");
      ModelAndView modelAndView = new ModelAndView("user", "command", user);
      return modelAndView;
   }

   @RequestMapping(value = "/addUser", method = RequestMethod.POST)
   public String addUser(@ModelAttribute("SpringWeb")User user, 
      ModelMap model) {
      model.addAttribute("username", user.getUsername());
      model.addAttribute("password", user.getPassword());
      model.addAttribute("address", user.getAddress());
      model.addAttribute("receivePaper", user.isReceivePaper());
      model.addAttribute("favoriteFrameworks", user.getFavoriteFrameworks());
      model.addAttribute("gender", user.getGender());
      return "userlist";
   }

   @ModelAttribute("webFrameworkList")
   public List<String> getWebFrameworkList()
   {
      List<String> webFrameworkList = new ArrayList<String>();
      webFrameworkList.add("Spring MVC");
      webFrameworkList.add("Spring Boot");
      webFrameworkList.add("Struts 2");
      webFrameworkList.add("Apache Hadoop");
      return webFrameworkList;
   }
}

  現在對userController.java進行說明:

public List<String> getWebFrameworkList()這一項是多選復選框的所有選項 ,@ModelAttribute("webFrameworkList")中的webFrameworkList是jsp頁面中可以自己調用的屬性變量.

而public ModelAndView user()代碼塊中的:

User user = new User();      
user.setFavoriteFrameworks((new String []{"Spring MVC","Struts 2"}));

  是默認勾選選。即一打開頁面,Spring MVC合影Struts 2就好被勾選.

 

user.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
   <title>Spring MVC表單處理(單選框)</title>
</head>
<body>

<h2>用戶信息 - </h2>
<form:form method="POST" action="/RadioButton/addUser">
   <table>
      <tr>
         <td><form:label path="username">用戶名:</form:label></td>
         <td><form:input path="username" /></td>
      </tr>
      <tr>
         <td><form:label path="password">密碼:</form:label></td>
         <td><form:password path="password" /></td>
      </tr>  
      <tr>
         <td><form:label path="address">地址:</form:label></td>
         <td><form:textarea path="address" rows="5" cols="30" /></td>
      </tr>  
      <tr>
         <td><form:label path="receivePaper">訂閱新聞?</form:label></td>
         <td><form:checkbox path="receivePaper" /></td>
      </tr> 
   <tr>
		<td><form:label path="favoriteFrameworks">喜歡的技術</form:label></td>
		<td><form:checkboxes items="${webFrameworkList}" path="favoriteFrameworks" /></td>
	</tr>
      <tr>
         <td colspan="2">
            <input type="submit" value="提交"/>
         </td>
      </tr>
   </table>  
</form:form>
</body>
</html>

  userlist.jsp代碼

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<!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>用戶信息顯示</title>
</head>
<body>


<table>
	<tr>
		<td>用戶名</td>
		<td>${username}</td>
	</tr>
	<tr>
		<td>密碼</td>
		<td>${password}</td>
	</tr>
	<tr>
		<td>地址</td>
		<td>${address}</td>
	</tr>
	<tr>
		<td>是否訂閱</td>
		<td>${receivePaper}</td>
	</tr>
	<tr>
		<td>喜歡的技術</td>
		<td>
			<%
				String[] favoriteFrameworks = (String[])request.getAttribute("favoriteFrameworks");
				for(String framework: favoriteFrameworks)
				{
					out.println(framework);
				}
			%>
		</td>
	</tr>
	
</table>


</body>
</html>

  

注:解析變量一定要有一下標識:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM