SpringMVC:form标签
form标签主要用于页面的表单。
pom.xml:
导入SpringMVC依赖
web.xml:
配置SpringMVC的 入口servlet,由servlet分配任务
springmvc.xml:
基础配置。
首页的:
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form:form action="find.action" method="post" modelAttribute="user">
<%--
<form:select path="cid" items="${cityList}" itemValue="cid" itemLabel="cname"></form:select>
--%>
<%--<form:radiobuttons path="cid" items="${cityList}" itemValue="cid" itemLabel="cname"></form:radiobuttons>
--%>
<form:checkboxes path="cids" items="${cityList}" itemValue="cid" itemLabel="cname"></form:checkboxes>
<hr>
<form:select path="pid" items="${provices}"></form:select>
<hr>
<form:checkboxes path="love" items="${loves}"></form:checkboxes>
<hr>
<a href="first.action">first</a>
<form:select path="area">
<form:option value="英国">英国</form:option>
<form:option value="中国">中国</form:option>
<form:option value="美国">美国</form:option>
<form:option value="日国">日国</form:option>
</form:select>
<hr>
true?:<form:checkbox path="marry"></form:checkbox>
<hr>
like: <form:checkbox path="hobby" value="like"></form:checkbox>
love: <form:checkbox path="hobby" value="love"></form:checkbox>
look: <form:checkbox path="hobby" value="look"></form:checkbox>
单选框:
女: <form:radiobutton path="sex" value="女"></form:radiobutton>
男:<form:radiobutton path="sex" value="男"></form:radiobutton>
<%--必须放在form内--%>
<hr>
<input type="submit" value="find">
</form:form>
</body>
</html>
组件主要是:单选框、多选框、下拉框
<form:form action="find.action" method="post" modelAttribute="user">:modelAttribute是传过来的对象,path是对象属性
<form:radiobuttons>:单选
path:即对象的属性名
items:遍历传参
itemValue:id
itemLabel:文本
controller:
package com.hc.controller;
import com.hc.model.City;
import com.hc.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class FirstController {
@RequestMapping("find")
public String find(User user) {
// System.out.println(user.getCid());
for (int s : user.getCids()) {
System.out.println(s+"这个大号");
}
// for (String s : user.getLove()) {
// System.out.println(s+"这个大号");
// }
return "redirect:first.action";
}
@RequestMapping("first")
public String first(ModelMap map) {
List<City> cityList = new ArrayList<>();
cityList.add(new City(1, "北京"));
cityList.add(new City(2, "天津"));
cityList.add(new City(3, "重庆"));
List<String> loves = new ArrayList<>();
loves.add("吃饭");
loves.add("逛街");
loves.add("花钱");
Map<Integer, String> provices = new HashMap<>();
provices.put(1, "华南");
provices.put(2, "湖南");
provices.put(3, "广西");
User user = new User();
user.setCids(new int[]{1});
user.setPid(2);
user.setCid(2);
user.setLove(new String[]{"吃饭", "花钱"});
user.setArea("中国");
user.setMarry(true);
user.setSex("男");
user.setHobby(new String[]{"like", "love"});
map.addAttribute("user", user);
map.addAttribute("loves", loves);
map.addAttribute("cityList", cityList);
map.addAttribute("provices", provices);
return "first";
}
}
ModelMap :模型Map
map.addAttribute("loves", loves):传参
属性配置:
public class User {
private boolean marry;
private String sex;
private String[] hobby;
private String area;
private String[] love;
private int cid;
private int pid;
private int[] cids;
}
解决选择框选中的问题。
无需Ajax