1.从项目导入开始,自己创建一个webapp的java模块,把资料项目复制进去,记得修改resources文件作为resources root,并且删除index,jsp,注意自己配置项目时,要在xml文件中写逻辑试图上下文,记得要创建新的项目服务器
2.进入index.html页面查看是否在根标签添加
<html lang="en" xmlns:th="http://www.thymeleaf.org">
并且设置动态上下文标签
<base th:href="@{/}" >
3.首先说明,在该项目中页面全部放在WEB-INF文件夹下,及对浏览器隐藏,无法直接访问,所以页面跳转都得通过跳转函数
4.首先编写页面中的跳转功能
<a href="user?method=toRegist" class="register">注册</a>
页面跳转函数在UserServlet中
//跳至注册页
protected void toRegist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.processTemplate("user/regist",req,resp);
}
点击链接跳转至注册页面,此时页面逻辑为:收集页面input框中的信息,存入User类的对象user中,直接用BeanUtil方法,然后调用service层的注册函数,service层的注册函数为user中的密码加密并存入user,接着调用Dao层的注册函数,Dao层的注册函数执行sql语句进行相依的数据库操作,获得返回值,判断返回值row>0则数据库添加数据成功,重定向至登陆页面,否则转发至当前页面。
(注意:注册成功重定向时,链接不能直接上下文加页面名,原因上面讲过,要使用上下文加上跳转至跳转函数的形式,之后重定向时都须注意)
protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user = new User();
try {
BeanUtils.populate(user,req.getParameterMap());
} catch (Exception e) {
System.out.println("页面数据存入user中时出现错误!");
e.printStackTrace();
throw new RuntimeException(e);
}
System.out.println("user中的内容:"+user);
int row = userService.addUser(user);
System.out.println("影响了"+row+"行");
if (row>0){
resp.sendRedirect(req.getContextPath() + "/user?method=toLogin");
}else {
this.processTemplate("user/regist",req,resp);
}
}
5.首页中的登录链接
<a href="user?method=t