基于springmvc的简单增删改查实现---中间使用到了bean validation

时间:2024-08-13 22:34:38
package com.kite.controller;

import java.util.HashMap;
import java.util.Map; import javax.validation.Valid; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.kite.entity.User; /**
* 简单的增删改查实现
* @author kite
*
*/
@Controller
@RequestMapping(value={"/user"})
public class UserController
{
//简单数据模拟
Map<String, User> map = new HashMap<String, User>();
public UserController()
{
map.put("zs", new User("zs", "", "张三", "123dsad456"));
map.put("ll", new User("ll", "", "李四", "123dsa456"));
map.put("ww", new User("ww", "", "王五", "dsa"));
map.put("zl", new User("zl", "5dsada", "赵六", "dsadsa"));
}
/**
* 显示列表
* @param model
* @return
*/
@RequestMapping({"/list"})
public String list(Model model)
{
model.addAttribute("users", map);
return "user/list";
}
/**
* 到添加页面使用method请求的方式来区分
* @return
*/
@RequestMapping(value={"/add"},method=RequestMethod.GET)
public String add(User user)
{
return "/user/add";
}
/**
* post请求方式实现真正的添加
* 使用bean validation
* BindingResult br 必须在@valid后面
* @return
*/
@RequestMapping(value={"/add"}, method=RequestMethod.POST)
public String add(@Validated User user, BindingResult br)
{
if(br.hasErrors())
{
//出错
return "user/add";
}
map.put(user.getUserName(), user);
return "redirect:/user/list";
}
/**
* 查看
* @param userName
* @param model
* @return
*/
@RequestMapping({"/{userName}/show"})
public String show(@PathVariable String userName, Model model)
{
User user = map.get(userName);
model.addAttribute(user);
return "/user/show";
}
/**
* 到修改页面
* @param userName
* @return
*/
@RequestMapping(value={"/{userName}/update"},method=RequestMethod.GET)
public String update(@PathVariable @ModelAttribute(value="userName") String userName, Model model)
{
User user = map.get(userName);
model.addAttribute(user);
return "/user/update";
}
/**
* 真正的更新
* @param userName
* @param user
* @param br
* @return
*/
@RequestMapping(value={"/{userName}/update"},method=RequestMethod.POST)
public String update(@PathVariable String userName, @Valid User user, BindingResult br)
{
map.put(userName, user);
return "redirect:/user/list";
}
/**
* 删除的实现
* @param userName
* @return
*/
@RequestMapping(value={"/{userName}/delete"},method=RequestMethod.GET)
public String delete(@PathVariable String userName)
{
map.remove(userName);
return "redirect:/user/list";
}
} 使用到bean validation的类 注解放在get方法只上
package com.kite.entity; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range; public class User
{
private String userName;
private String password;
private String nickName;
private String email;
public User()
{
super();
// TODO Auto-generated constructor stub
}
public User(String userName, String password, String nickName, String email)
{
super();
this.userName = userName;
this.password = password;
this.nickName = nickName;
this.email = email;
}
@Override
public String toString()
{
return "User [userName=" + userName + ", password=" + password
+ ", nickName=" + nickName + ", email=" + email + "]";
}
@NotEmpty(message="用户名不能为空")
public String getUserName()
{
return userName;
} public void setUserName(String userName)
{
this.userName = userName;
}
@Size(min=,max=,message="密码的长度必须在6-18之间")
public String getPassword()
{
return password;
} public void setPassword(String password)
{
this.password = password;
}
public String getNickName()
{
return nickName;
}
public void setNickName(String nickName)
{
this.nickName = nickName;
}
@Email(message="邮箱格式不符合")
public String getEmail()
{
return email;
} public void setEmail(String email)
{
this.email = email;
} } 其他代码就不一一复制了