package com.yourcompanyname;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.hibernate.SessionFactory;
public class AdminDAOFactory {
Session session;
Transaction tx;
public void add(Admin admin) throws HibernateException {
/**
* Creation Date: 11-17-2005
* TODO Add a new admin user.
* @param An object of Admin
* @return void
* @author Coder Guo
*/
try {
session = SessionFactory.currentSession();
tx = session.beginTransaction();
//Add a new admin
session.save(admin);
tx.commit ();
}catch(HibernateException e){
throw e;
}finally{
if (tx!=null) {
tx.rollback();
}
SessionFactory.closeSession();
}
}
}
再打开com.yourcompany.struts.action下的AddAdminAction.java添加(其中如果有错误选中好按ctrl+shift+o自动添加包)
public class AddAdminAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @author Coder Guo
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
AddAdminForm addAdminForm = (AddAdminForm) form;
// TODO Add a new admin
Admin admin = new Admin();
admin.setName(addAdminForm.getName ());
admin.setPassword(addAdminForm.getPassword ());
AdminDAOFactory adminDAO = new AdminDAOFactory ();
adminDAO.add(admin);
return mapping.findForward("success");
}
}
再打开com.yourcompanyname.struts.form下的AddAdminForm.java,修改(如果有错误按照上面说的方法导入包)
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors errors = new ActionErrors();
Session session = SessionFactory.currentSession();
Transaction tx = session.beginTransaction ();
Query query = session.createQuery("select admin from Admin as admin where admin.name = '" + this.name + "'");
Iterator it = query.iterate ();
if (it.hasNext()){
errors.add ("addAdmin.err.name",new ActionMessage("form.addAdmin.err.name"));
}
tx.commit();
SessionFactory.closeSession ();
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
this.name=null;
this.password=null;
}
再打开com\yourcompanyname\struts下的ApplicationResource.properties在这里面添加错误信息:
Form.addAdmin.err.name=err
最后,(汗,好累啊-_-!)打开addAdmin.jsp修改成如下:
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page language="java"%>
<%@ taglib uri="
http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="
http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<script language = "javascript">
<!--
function check(){
if (loginForm.userName.value == "" || loginForm.password.value == ""){
alert("请输入完整的信息!");
loginForm.userName.focus();
return false;
}
}
//-->
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<html:base />
<title>login.jsp</title>
<link href="css/webcss.css" rel="stylesheet" type="text/css">
<meta http-equiv="pragma" content="no- cache">
<meta http-equiv="cache-control" content="no- cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http- equiv="description" content="This is my page">
</head>
<body>
<center>
<p> </p>
<p> </p>
<table width="300" border="0" cellpadding="0" cellspacing="0">
<html:form action="/addAdmin" focus="name" method="GET">
<tr align="center" valign="middle">
<td colspan="2" class="typt_normal">新增管理员</td>
</tr>
<tr>
<td width="100" align="center" valign="middle" class="typt_normal">名称: </td>
<td width="200" align="left"><html:text property="name" styleClass="text_s"/><html:errors property="addAdmin.err.name"/></td>
</tr>
<tr>
<td width="100" align="center" valign="middle" class="typt_normal">密码: </td>
<td width="200" align="left"><html:password property="password" styleClass="text_s"/></td>
</tr>
<tr>
<td colspan="2" align="center" valign="middle"><html:submit value="提交"/><html:reset value="重置"></html:reset></td>
</tr>
</html:form>
</table>
</center>
</body>
</html:html>
其中可以看到如何在struts的标签中使用javascript的方法。
配置好myeclipse于tomcat的连接。在window->Preferences做如下设定:
在项目文件点右键->“myeclipse”->“Add and remove project deployment”,如下图:
好了,我们的配置工作基本结束了,在myeclipse上开启tomcat服务
现在打开浏览器,输入
http://127.0.0.1:8080/test/addAdmin.jsp就可以看到你的jsp页面了!