jdbc:mysql://localhost:3306/javassh?useUnicode=true&characterEncoding=UTF-8
2.2.1注意取消生成factory选项
2.2.2增加hibernate的属性
format_sql=true
hbm2ddl.auto=update
2.3.1注意勾选JDBC、Core、Web、Testing(可选)这几个选项
2.3.2把工程里面的applicationContext.xml改名为spring.xml
2.3.2在src目录下拷入log4j:log4j.properties,配置为debug
17SSH_SpringHibernate
contextConfigLocation
classpath:spring.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
springmvc
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
CharacterEncodingFilter
/*
*.jsp
true
3.2.1在src目录下新建spring-mvc.xml文件,可以复制spring.xml完成
3.2.2spring-mvc.xml文件代码如下:
3.2.3 说明:此时的工程结构如图:
@RequestMapping(method=RequestMethod.GET)
public String get(){
return "login";
}
@RequestMapping(method=RequestMethod.POST)
public String post(){
return "login_success";
}
4.2.1拷入bootstrap和jquery到res目录下
4.2.2在jsp页面中导入bootstrap和jquery,并编写jsp页面
4.2.3增加验证码
private String username;
private String password;
private String vcode;
4.3.1 Hibernate-valiation相应的jar包
4.3.2在login这个bean上添加如下注解(部分):
@NotNull
@Length(min=3, max=16)
private String username;
@NotNull
private String password;
@NotNull
private String vcode;
4.3.3在控制器(LoginController)上添加如下代码:
@RequestMapping(method=RequestMethod.POST)
public String post(@Valid@ModelAttribute("login")Login login,
BindingResult errors){
if(errors.hasErrors()){
return "login";
}
return "login_success";
}
4.3.4在login.jsp页面上添加显示错误消息的代码:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
//注意,先要通过post的参数传一个session进来
String sessionVcode = (String) session.getAttribute(ValidateCode.VCODE_SESSION_KEY);
if(sessionVcode == null || !sessionVcode.toLowerCase().equals(login.getVcode().toLowerCase())){
errors.addError(new FieldError("login", "vcode", "验证码错误"));
}
org.hibernate.dialect.MySQLDialect
true
update
private Integer id;
private String username;
private String password;
private String name;
private String nickname;
/*给出对应地get和set方法*/
public interface UserDao {
public User login(String username, String password);
public Integer save(User user);
public void update(User user);
public boolean validatePassword(Integer id, String password);
public void updatePassword(Integer id, String password);
}
@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public User login(String username, String password) {
List users = getHibernateTemplate().find("from User where username=? and password=?", username, password);
return users.size() > 0 ? users.get(0) : null;
}
@Override
public Integer save(User user) {
return (Integer) getHibernateTemplate().save(user);
}
@Override
public void update(User user) {
getHibernateTemplate().update(user);
}
@Override
public boolean validatePassword(Integer id, String password) {
return false;
}
@Override
public void updatePassword(Integer id, String password) {
}
}
public interface UserService {
public User login(String username, String password);
public Integer save(User user);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
@Transactional(readOnly=true)
public User login(String username, String password) {
return userDao.login(username, password);
}
@Override
public Integer save(User user) {
return userDao.save(user);
}
}
@Autowired
private UserService userService;
User user = userService.login(login.getUsername(), login.getPassword());
if(user == null){
errors.addError(new ObjectError("login", "用户名或者密码错误"));
return "login";
}else{
//这里的SessionUser是为了把用户登录信息放到session中
SessionUser sessionUser = SessionUser.fromUser(user);
session.setAttribute(SessionUser.class.getSimpleName(), sessionUser);
}
return "login_success";
本文链接:http://blog.csdn.net/tiandixuanwuliang/article/details/72598938