一.页面 jsp 动态化
1、在 html 页面顶行添加 page 指令。
2、修改文件后缀名为:.jsp。
3、使用 IDEA 搜索替换.html 为.jsp(快捷键:Ctrl+Shift+R).
二.抽取页面中相同的内容
1.head 中 css、jquery、base.
2.每个页面的页脚.
3.登录成功后的菜单.
4.manager 模块的菜单。
三. 登录,注册错误提示,及表单回显
四.BaseServlet 的抽取
在实际的项目开发中,一个模块,一般只使用一个 Servlet 程序。
代码优化一:代码优化:合并 LoginServlet 和 RegistServlet 程序为 UserServlet。
在jsp文件中添加隐藏域。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("login".equals(action)){
login(request,response);
}else if ("regist".equals(action)){
regist(request,response);
}
}
/**
* 登录的功能
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//登录的代码
}
/**
* 注册的功能
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//注册的代码
}
优化代码二:使用反射优化大量 else if 代码。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
try {
//获取action业务,鉴别字符串,获取相应的业务,方法反射对象
Method declaredMethod = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
//调用目标业务
declaredMethod.invoke(this,request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
代码优化三:抽取 BaseServlet 程序
public abstract class BaseServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
try {
//获取action业务,鉴别字符串,获取相应的业务,方法反射对象
Method declaredMethod = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
//调用目标业务
declaredMethod.invoke(this,request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
五.数据的封装和抽取 BeanUtils 的使用
BeanUtils 工具类,它可以一次性的把所有请求的参数注入到 JavaBean 中。
BeanUtils 工具类,经常用于把 Map 中的值注入到 JavaBean 中,或者是对象属性值的拷贝操作。 BeanUtils 它不是 Jdk 的类。而是第三方的工具类。所以需要导包。
1、导入需要的 jar 包: commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar
2、编写 WebUtils 工具类使用。
注入调用属性的set方法。
把参数HttpServlet req 改成Map value:
把map的值注入javabean是很常见的。
如果写成HttpServlet req,DAO层和Service层不能使用此方法。
写成Map value,在Web层,Service层,DAO层都可以使用。适用范围更广。
降低代码耦合。
并使用泛型。
User user = WebUtils.copyParamToBean(request.getParameterMap(), new User());
public class WebUtils {
/**
* 把Map中的值注入到对应的JavaBean属性中。
* @param value
* @param bean
*/
public static <T> T copyParamToBean( Map value , T bean ){
try {
System.out.println("注入之前:" + bean);
/**
* 把Map所有请求的参数都注入到user对象中
*/
BeanUtils.populate(bean, value);
System.out.println("注入之后:" + bean);
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
}
使用 EL 表达式修改表单回显。
login:
${ empty requestScope.msg ? "请输入用户名和密码":requestScope.msg }
value="${requestScope.username}
regist:
${ requestScope.msg }
value="${requestScope.username}