1.1版本的自定义框架
首先在Hibernate的基础下,将多个Servlet合并成了一个ServletAction,此时ServletAction只负责做判断调用对应的Action类,Action类则负责从jsp页面拿值处理调用Biz,并返回调用的下一个jsp页面给ServletAction做出对应的处理.
以登录为例子
ServletAction
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
String path=request.getRequestURI();
String actionName=path.substring(path.lastIndexOf("/")+1,path.lastIndexOf("."));
Action action=null;
if(actionName.equals("login")){
action=new LoginAction();
}
String resultView=action.execute(request, response);
request.getRequestDispatcher(resultView).forward(request, response);
}
LoginAction
public String execute(HttpServletRequest request,
HttpServletResponse response) {
String uname=request.getParameter("uname");
String upass=request.getParameter("upass");
Info u=new Info();
u.setUname(uname);
u.setUpass(upass);
UserBiz userBiz=new UserBizImpl();
Info user=userBiz.findUser(u);
if(user!=null){
return "show.jsp";
}else{
request.setAttribute("msg", "用户名密码错误!");
return "index.jsp";
}
}
对应的jsp页面代码则是
<body>
${msg }
<form name="f1" id="f1" action="login.cc" method="post">
<table border="0">
<tr>
<td>Login:</td>
<td><input type="text" name="uname" id="login"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="upass" id="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="login user"></td>
</tr>
</table>
</form>
</body>