我正在使用JSTL标记库和具有模型属性的映射,如下面的代码所示。
Index.html
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<c:url var="resourceURL" value="/resources" />
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<form:form method="post" modelAttribute="userLogin">
<table>
<tr>
<td>hobby</td>
<td>cricket<form:checkbox path="hobby" value="cricket" />
soccer<form:checkbox path="hobby" value="soccer" />
tennis<form:checkbox path="hobby" value="tennis" />
hockey<form:checkbox path="hobby" value="hockey" />
</td>
<td><form:errors path="hobby" cssClass="error" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="update"></td>
</tr>
</table>
</form:form>
</body>
</html>
MainController.java
package com.test.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import com.test.domain.UserLogin;
import com.test.service.UserLoginService;
@Controller
public class MainController {
@Autowired
private UserLoginService uls;
@GetMapping
public String landingIndex(Model model) {
model.addAttribute("userLogin", new UserLogin());
return new String("index");
}
@PostMapping
public String insertingValue(@ModelAttribute @Valid UserLogin userLogin01, BindingResult result) {
if(result.hasErrors()) {
return new String("index");
}else {
uls.saveUser(userLogin01);
}
return new String("redirect:/home");
}
@GetMapping("/home")
public ModelAndView redirectingToHome(){
ModelAndView mv = new ModelAndView("Home");
mv.addObject("userList",uls.fetchingUsersDetails());
return mv;
}
@GetMapping("/delete")
public String deleteUserName(@ModelAttribute UserLogin userLogin){
uls.deletingUser(userLogin);
return new String("redirect:/home");
}
@GetMapping("/update")
public ModelAndView updateUserName(@ModelAttribute UserLogin userLogin,Model model){
ModelAndView mv = new ModelAndView("index");
model.addAttribute("userLogin", uls.fetchingUserDetailThroughId(userLogin));
return mv;
}
}
DaoImpl.java
package com.test.daoimpl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.test.dao.UserLoginDao;
import com.test.domain.UserLogin;
@Repository
@Transactional
public class UserLoginDaoImpl implements UserLoginDao{
@Autowired(required = true)
private SessionFactory sessionFactory;
public void saveUser(UserLogin ul) {
sessionFactory.getCurrentSession().saveOrUpdate(ul);
}
@SuppressWarnings("unchecked")
@Override
public List<UserLogin> fetchingUsersDetails() {
return sessionFactory.getCurrentSession().createCriteria(UserLogin.class).list();
}
@Override
public void deletingUser(UserLogin ul) {
sessionFactory.getCurrentSession().delete(ul);
}
@Override
public UserLogin fetchingUserDetailThroughId(UserLogin ul) {
return sessionFactory.getCurrentSession().get(UserLogin.class,ul.getId());
}
}
**output**
在数据库中,爱好插入如下: 板球,足球,曲棍球
但是在更新时,我没有任何复选框。但是如果我在数据库中只有一个爱好,那么就检查相应的数据库。