<form:select>的使用

最近在学习springMVC,用到了<form:select>标签,使用发过程中遇到了些问题,现在记录下,以防忘记。

我jsp页面是这样的:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> //使用之前要记得加
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'bookList.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-1.9.1.js"></script>
<style type="text/css">
table {
width: 80%;
height: auto;
border: 2px solid black;
}

body {
text-align: center;
}
</style>
<script type="text/javascript">
$(function(){
alert(${list});
});
</script>
</head>
<body>
<form:form action="" method="post" modelAttribute="books">//一开始没有添加这个属性,一直报错,“books” 是与controller中map添加的一个“books”的键值对相对应
<table class="btnTable" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>书名</td>
<td><form:select id="name" path="name" >//"path"的值就是“books”的属性名
<form:options items="${booksQueryList }" itemLabel="name" itemValue="name"/>
</form:select></td>
<td>作者</td>
<td><form:select path="author" items="${booksQueryList}"
itemValue="author" itemLabel="author"></form:select></td>
<td>类型</td>
<td><form:select path="type" items="${booksQueryList}"
itemLabel="type" itemValue="type"></form:select></td>
<td >
<button type="button" action="booksQueryList">查询</button>
&nbsp&nbsp
<button type="reset" >清空</button>
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<td>书名</td>
<td>类型</td>
<td>作者</td>
<td>剩余数量</td>
<td>被借次数</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<tr>
<c:forEach items="${ booksList}" var="b">
<td>${b.name }</td>
<td>${b.type }</td>
<td>${b.author }</td>
<td>${b.discount }</td>
<td>${b.haslended }</td>
<td><a href="#">编辑</a></td>
</c:forEach>
</tr>
</tbody>
</table>
</form:form>
</body>
</html>

 

我的controller是这样的:

@Controller
public class UserController {
@Autowired
private UserService service;
@Autowired
private BooksService booksService;
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
@RequestMapping(value="register")
public String register(String name,String password,Map<String, Object> params){
Users users=new Users();
users.setName(name);
users.setPassword(password);
users.setPoint(0);
users.setType(0);
users.setLevel(0);
service.save(users,params);
params.put("userName", name);
if(params.get("ErrMsg")!=null){
return "register";
}
return "reg_success";
}

@RequestMapping(value="login")
public String login(Users users,Map<String, Object> map){
Users users2 = service.findUsers(users);
if(users2==null){
map.put("ErrMsg","输入有误,请重新输入");
return "";
}
map.put("users", users2);
if(users2.getType()==1){
List<Books> booksList = booksService.findAllBooks();
map.put("booksQueryList", booksList);
map.put("books", new Books()); //这个“books”其实是new的一个Books对象,添加的目的应该就是为jsp中的“path”服务的
return "bookList";
}
return "log_success";
}
}

 

这是我的第一个操作记录博客,对于专业人员来说应该有很多错误和不足,希望热心的朋友批评指正,努力努力再努力!

 

posted @ 2016-12-29 10:28  yuqiaopeng  阅读(35368)  评论(0编辑  收藏  举报