您的位置:首页 > 其它

Hibernate综合运用内部留言本(二)

2017-12-01 19:08 281 查看
一 理解需求

二 根据需求文档,画出程序框架图

三 创建一个web项目

四 创建web层。
1 引入struts
2 web层开发
2.1 login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<h1>用户登录</h1>
<form action="/NoteBook/login.do?flag=login" method="post">
<table>
<tr><td>用户id</td><td><input type="text" style="width: 100px" name="userid" /></td></tr>
<tr><td>用户pw</td><td><input type="password" style="width: 100px" name="userpwd" /></td></tr>
<tr><td><input type="submit" value="登录" /></td><td><input type="reset" value="重新填写" /></td></tr>
</table>
</form>
</body>
</html>


2.2 showMessage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"; prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html>
<head>
<title>My JSP 'showMessage.jsp' starting page</title>
</head>

<body>
<font size="6"><b><a href="#">发布信息</a></b></font>  
<font size="6"><b><a href="/NoteBook/login.do?flag=logout">退出系统</a></b></font><br/>
欢迎${userinfo.name } 留言信息:
<table width="500px">
<tr><td>发送人</td><td>发送时间</td><td>接收人</td><td>信息内容</td></tr>
<c:forEach items="${messageList}" var="message">
<tr>
<td>${message.sender.name }</td>
<td>${message.mesTime }</td>
<td>${message.getter.name }</td>
<td>${message.content }</td>
</tr>
</c:forEach>
</table>
</body>
</html>


2.3 LoginAction
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.sina.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.sina.domain.Users;
import com.sina.service.imp.UsersServiceImp;
import com.sina.service.inter.UsersServiceInter;
import com.sina.struts.form.UserForm;
/**
* MyEclipse Struts
* Creation date: 05-30-2011
*
* XDoclet definition:
* @struts.action parameter="flag"
*/
public class LoginAction extends DispatchAction {
/*
* Generated Methods
*/
/**
* 这里我们响应login请求
*/
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
UserForm userForm=(UserForm)form;
//使用service完成到数据库的验证
//    UsersServiceImp usersService=new UsersServiceImp();//?这里直接使用对象,有定义耦合 web<-->业务层
//通过接口来调用.
UsersServiceInter usersServiceInter= new UsersServiceImp();
//构建一个Users对象
Users user=new Users();
//String ->Integer
user.setUserid(Integer.parseInt(userForm.getUserid()));
user.setUserpwd(userForm.getUserpwd());
user=usersServiceInter.checkUser(user);
if(user!=null){
//合法,将user对象放入session,后有用
request.getSession().setAttribute("userinfo", user);
return  mapping.findForward("loginok");
}else{
return  mapping.findForward("goLoginUi");
}

}
/**
* 这里我们响应logout请求
*/
public ActionForward logout(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//清空session
request.getSession().invalidate();
return mapping.findForward("goLoginUi");

}
}


2.4 GoMessageUiAction
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.sina.struts.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.sina.domain.Message;
import com.sina.domain.Users;
import com.sina.service.imp.MessageServiceImp;
import com.sina.service.inter.MessageServiceInter;
/**
* MyEclipse Struts
* Creation date: 05-30-2011
*
* XDoclet definition:
* @struts.action parameter="flag"
*/
public class GoMessageUiAction extends DispatchAction {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
//去获取应该显示的留言信息,完成显示
//取出该用户收到的信息
MessageServiceInter messageServiceInter=new MessageServiceImp();
//取出当前用户
Users loginUser=(Users) request.getSession().getAttribute("userinfo");
List<Message> list=messageServiceInter.showMessage(loginUser);

//现在应当list集合放到 session request, application? request
request.setAttribute("messageList", list);

return mapping.findForward("goMessageUi");
}
}


2.5 UserForm
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.sina.struts.form;
import org.apache.struts.action.ActionForm;
/**
* MyEclipse Struts
* Creation date: 05-30-2011
*
* XDoclet definition:
* @struts.form name="userForm"
*/
public class UserForm extends ActionForm {
private String userid;
private String userpwd;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}


2.6 struts-config.xml


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hibernate