1.第三阶段:
1.1页面 jsp 动态化
1、在 html 页面顶行添加 page 指令。
2、修改文件后缀名为:.jsp
3、使用 IDEA 搜索替换.html 为.jsp(快捷键:Ctrl+Shift+R)
1.2抽取页面中相同的内容
i. head 中 css、jquery、base 标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 动态的base标签值 --%>
<%
String basePath = request.getScheme()
+ "://"
+ request.getServerName()
+ ":"
+ request.getServerPort()
+ request.getContextPath()
+ "/";
%>
<!-- 写base标签,永远固定相对路径跳转结果 -->
<base href="<%= basePath%>">
<link type="text/css" rel="stylesheet" href="static/css/style.css" >
<script type="text/javascript" src="static/script/jquery-1.7.2.js"></script>
ii. 每个页面的页脚
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div id="bottom">
<span>
尚硅谷书城.Copyright ©2015
</span>
</div>
iii. 登录成功后的菜单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div>
<span>欢迎<span class="um_span">韩总</span>光临尚硅谷书城</span>
<a href="../order/order.jsp">我的订单</a>
<a href="../../index.jsp">注销</a>
<a href="../../index.jsp">返回</a>
</div>
iv. manager 模块的菜单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div>
<a href="pages/manager/book_manager.jsp">图书管理</a>
<a href="pages/manager/order_manager.jsp">订单管理</a>
<a href="../../index.jsp">返回商城</a>
</div>
1.3 登录,注册错误提示,及表单回显
jsp 页面,需要输出回显信息
1.4 BaseServlet 的抽取
在实际的项目开发中,一个模块,一般只使用一个 Servlet 程序。
代码优化一:代码优化:合并 LoginServlet 和 RegistServlet 程序为 UserServlet 程序
UserServlet 程序:
public class UserServlet extends BaseServlet {
private UserService userService = new UserServiceImpl();
protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求的参数
String username = req.getParameter("username");
String password = req.getParameter("password");
//2.调用userService.login()处理登录业务
User loginUser = userService.login(new User(null, username, password, null));
//如果等于null,则登录失败
if (loginUser == null){
//把错误信息,和回显的表单项信息,保存到request域中
req.setAttribute("msg", "用户名或密码错误!");
req.setAttribute("username", username);
//跳回登录页面
req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
} else {
System.out.println("登录成功!");
req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp);
}
}
protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求的参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String email = req.getParameter("email");
String code = req.getParameter("code");
User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());
System.out.println(user);
//2.检查验证码是否正确,先写死,abcd
if ("abcd".equalsIgnoreCase(code)){
//正确
//3.检查用户名是否可用
if (userService.existsUsername(username)){
//把回显信息保存到request域中
req.setAttribute("msg", "用户名已存在!!!");
req.setAttribute("username", username);
req.setAttribute("email", email);
//跳回注册页面
req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
} else {
//可用, 调用service保存到数据库
userService.register(new User(null, username, password, email));
//跳转到注册成功的页面
req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp);
}
} else {
System.out.println("验证码:" + code + "错误");
req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
}
}
}
还要给 login.jsp 添加隐藏域和修改请求地址
给 tegist.jsp 页面添加隐藏域 action,和修改请求地址
优化代码二:使用反射优化大量 else if 代码:
代码优化三:抽取 BaseServlet 程序。
BaseServlet 程序代码:
public class BaseServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action");
try {
//通过action业务鉴别字符串,获取相应业务方法的反射对象
Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
//调用业务方法
method.invoke(this,req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
修改 UserServlet 程序继承 BaseServlet 程序。
1.5 数据的封装和抽取 BeanUtils 的使用
public class WebUtils {
public static <T> T copyParamToBean(Map value, T bean){
try {
System.out.println("注入之前:" + bean);
/**
* 把所有请求的参数都注入到user对象中
*/
BeanUtils.populate(bean, value);
System.out.println("注入之后:" + bean);
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
}
书城-第四阶段。使用 EL 表达式修改表单回显
以登录为示例: