login.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title><bean:message key="message.system.ui.loginForm.title"/></title>
</head>
<body>
<html:form action="/login">
<bean:message key="message.system.ui.loginForm.username"/>: <html:text property="stuName"/><html:errors property="stuName"/><br/>
<bean:message key="message.system.ui.loginForm.userpwd"/>: <html:text property="stuPwd"/><html:errors property="stuPwd"/><br/>
<html:submit><bean:message key="message.system.ui.loginForm.submit"/></html:submit>
<html:cancel><bean:message key="message.system.ui.loginForm.reset"/></html:cancel>
</html:form>
</body>
</html>
ApplicationResources.properties
# Resources for parameter 'org.rockie.ui.ApplicationResources'
# Project P/Studentdb
message.system.ui.loginForm.title=学生登陆界面
message.system.ui.loginForm.username=用户名
message.system.ui.loginForm.userpwd=密码
message.system.ui.loginForm.submit=登陆
message.system.ui.loginForm.reset=取消
errors.required=用户名或者密码错误
message.system.ui.showCoursesForm.allCourses=可选课程列表
Struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="org.rockie.db.Student" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
input="/form/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward
name="relogin"
path="/form/login.jsp"/>
<forward
name="selectCourse"
path="/selectCourse.do"/>
</action>
<action path="/selectCourse" type="org.springframework.web.struts.DelegatingActionProxy">
<forward
name="showall"
path="/form/showCourses.jsp"/>
</action>
<action name="loginForm" path="/mySelect" type="org.rockie.ui.action.MySelectAction" />
</action-mappings>
<message-resources parameter="org.rockie.ui.ApplicationResources" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
Student.java
package org.rockie.db;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.validator.ValidatorAction;
import org.apache.struts.validator.ValidatorActionForm;
/**
* Student generated by MyEclipse - Hibernate Tools
*/
public class Student extends ValidatorActionForm implements java.io.Serializable {
// Fields
private Integer stuId;
private String stuName;
private String stuPwd;
private Set courses=new HashSet();
// Constructors
public Set getCourses() {
return courses;
}
public void setCourses(Set courses) {
this.courses = courses;
}
/** default constructor */
public Student() {
}
/** full constructor */
public Student(String stuName, String stuPwd) {
this.stuName = stuName;
this.stuPwd = stuPwd;
}
// Property accessors
public Integer getStuId() {
return this.stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String getStuName() {
return this.stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuPwd() {
return this.stuPwd;
}
public void setStuPwd(String stuPwd) {
this.stuPwd = stuPwd;
}
}
LoginAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.rockie.ui.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.rockie.db.Student;
import org.rockie.db.StudentDAO;
/**
* MyEclipse Struts
* Creation date: 04-09-2007
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
*/
public class LoginAction extends Action {
private StudentDAO studentdao;
/*
* Generated Methods
*/
public StudentDAO getStudentdao() {
return studentdao;
}
public void setStudentdao(StudentDAO studentdao) {
this.studentdao = studentdao;
}
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
Student loginForm = (Student) form;// TODO Auto-generated method stub
List list=studentdao.findByExample(loginForm);
if(list.size()>0)
{
Student stu=(Student)list.get(0);
request.getSession().setAttribute("stuid", stu.getStuId());
return mapping.findForward("selectCourse");
}
else
{
return mapping.findForward("relogin");
}
}
}
StudentDAO.java
package org.rockie.db;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* Data access object (DAO) for domain model class Student.
* @see org.rockie.db.Student
* @author MyEclipse - Hibernate Tools
*/
public class StudentDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(StudentDAO.class);
//property constants
public static final String STU_NAME = "stuName";
public static final String STU_PWD = "stuPwd";
protected void initDao() {
//do nothing
}
public void save(Student transientInstance) {
log.debug("saving Student instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Student persistentInstance) {
log.debug("deleting Student instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Student findById( java.lang.Integer id) {
log.debug("getting Student instance with id: " + id);
try {
Student instance = (Student) getHibernateTemplate()
.get("org.rockie.db.Student", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Student instance) {
log.debug("finding Student instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Student instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Student as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByStuName(Object stuName) {
return findByProperty(STU_NAME, stuName);
}
public List findByStuPwd(Object stuPwd) {
return findByProperty(STU_PWD, stuPwd);
}
public Student merge(Student detachedInstance) {
log.debug("merging Student instance");
try {
Student result = (Student) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Student instance) {
log.debug("attaching dirty Student instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Student instance) {
log.debug("attaching clean Student instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static StudentDAO getFromApplicationContext(ApplicationContext ctx) {
return (StudentDAO) ctx.getBean("StudentDAO");
}
}
applicationContext.java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/studb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>rockie</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/rockie/db/Student.hbm.xml</value>
<value>org/rockie/db/Course.hbm.xml</value></list>
</property></bean>
<bean id="StudentDAO" class="org.rockie.db.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="CourseDAO" class="org.rockie.db.CourseDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean name="/selectCourse" class="org.rockie.ui.action.SelectCourseAction"
abstract="false" singleton="false" lazy-init="default"
autowire="default" dependency-check="default">
<property name="coursedao">
<ref bean="CourseDAO" />
</property>
</bean>
<bean name="/login" class="org.rockie.ui.action.LoginAction"
abstract="false" singleton="false" lazy-init="default"
autowire="default" dependency-check="default">
<property name="studentdao">
<ref bean="StudentDAO" />
</property>
</bean></beans>
Course.java
package org.rockie.db;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.validator.ValidatorAction;
import org.apache.struts.validator.ValidatorActionForm;
/**
* Course generated by MyEclipse - Hibernate Tools
*/
public class Course extends ValidatorActionForm implements java.io.Serializable {
// Fields
private Integer couId;
private String couName;
private String couDis;
private Set students=new HashSet();
// Constructors
public Set getStudents() {
return students;
}
public void setStudents(Set students) {
this.students = students;
}
/** default constructor */
public Course() {
}
/** full constructor */
public Course(String couName, String couDis) {
this.couName = couName;
this.couDis = couDis;
}
// Property accessors
public Integer getCouId() {
return this.couId;
}
public void setCouId(Integer couId) {
this.couId = couId;
}
public String getCouName() {
return this.couName;
}
public void setCouName(String couName) {
this.couName = couName;
}
public String getCouDis() {
return this.couDis;
}
public void setCouDis(String couDis) {
this.couDis = couDis;
}
}
SelectCourseAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.rockie.ui.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.rockie.db.CourseDAO;
/**
* MyEclipse Struts
* Creation date: 04-10-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class SelectCourseAction extends Action {
private CourseDAO coursedao;
/*
* Generated Methods
*/
public CourseDAO getCoursedao() {
return coursedao;
}
public void setCoursedao(CourseDAO coursedao) {
this.coursedao = coursedao;
}
/**
* 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
List list=coursedao.findAllCourses();
request.getSession().setAttribute("courses",list);
return mapping.findForward("showall");
}
}
CourseDAO.java
package org.rockie.db;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* Data access object (DAO) for domain model class Course.
* @see org.rockie.db.Course
* @author MyEclipse - Hibernate Tools
*/
public class CourseDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CourseDAO.class);
//property constants
public static final String COU_NAME = "couName";
public static final String COU_DIS = "couDis";
protected void initDao() {
//do nothing
}
public void save(Course transientInstance) {
log.debug("saving Course instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Course persistentInstance) {
log.debug("deleting Course instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public List findAllCourses() {
log.debug("getting all Courses");
try {
return this.getHibernateTemplate().find("from Course");
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public Course findById( java.lang.Integer id) {
log.debug("getting Course instance with id: " + id);
try {
Course instance = (Course) getHibernateTemplate()
.get("org.rockie.db.Course", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Course instance) {
log.debug("finding Course instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Course instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Course as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByCouName(Object couName) {
return findByProperty(COU_NAME, couName);
}
public List findByCouDis(Object couDis) {
return findByProperty(COU_DIS, couDis);
}
public Course merge(Course detachedInstance) {
log.debug("merging Course instance");
try {
Course result = (Course) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Course instance) {
log.debug("attaching dirty Course instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Course instance) {
log.debug("attaching clean Course instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static CourseDAO getFromApplicationContext(ApplicationContext ctx) {
return (CourseDAO) ctx.getBean("CourseDAO");
}
}
Course.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="org.rockie.db.Course" table="course">
<id name="couId" type="java.lang.Integer">
<column name="cou_id" />
<generator class="native" />
</id>
<property name="couName" type="java.lang.String">
<column name="cou_name" length="20" />
</property>
<property name="couDis" type="java.lang.String">
<column name="cou_dis" length="50" />
</property>
<set name="students" table="course_student_tabel" cascade="save-update" inverse="true">
<key column="cou_id"></key>
<many-to-many column="stu_id" class="org.rockie.db.Student"></many-to-many>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="org.rockie.db.Student" table="student">
<id name="stuId" type="java.lang.Integer">
<column name="stu_id" />
<generator class="native" />
</id>
<property name="stuName" type="java.lang.String">
<column name="stu_name" length="20" />
</property>
<property name="stuPwd" type="java.lang.String">
<column name="stu_pwd" length="20" />
</property>
<set name="courses" table="course_student_tabel" cascade="save-update" inverse="false">
<key column="stu_id"></key>
<many-to-many column="cou_id" class="org.rockie.db.Course"></many-to-many>
</set>
</class>
</hibernate-mapping>
AllCourseServlet.java
package org.rockie.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.rockie.db.CourseDAO;
public class AllCourseServlet extends HttpServlet {
private CourseDAO coursedao;
public CourseDAO getCoursedao() {
return coursedao;
}
public void setCoursedao(CourseDAO coursedao) {
this.coursedao = coursedao;
}
/**
* Constructor of the object.
*/
public AllCourseServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List list=coursedao.findAllCourses();
request.getSession().setAttribute("courses",list);
response.sendRedirect("/Studentdb/form/showCourses.jsp");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
MySelectAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.rockie.ui.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.rockie.db.Student;
/**
* MyEclipse Struts
* Creation date: 04-10-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class MySelectAction extends Action {
/*
* 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
Student student=(Student)form;
return null;
}
}
SelectCourseAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.rockie.ui.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.rockie.db.CourseDAO;
/**
* MyEclipse Struts
* Creation date: 04-10-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class SelectCourseAction extends Action {
private CourseDAO coursedao;
/*
* Generated Methods
*/
public CourseDAO getCoursedao() {
return coursedao;
}
public void setCoursedao(CourseDAO coursedao) {
this.coursedao = coursedao;
}
/**
* 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
List list=coursedao.findAllCourses();
request.getSession().setAttribute("courses",list);
return mapping.findForward("showall");
}
}
showCourses.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>showCourses.jsp</title>
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<html:form action="/mySelect" method="post">
<center><bean:message key="message.system.ui.showCoursesForm.allCourses"/></center><br>
<logic:iterate id="course" name="courses" scope="session">
<html:multibox property="courses">
<bean:write property="couId" name="course"/>
</html:multibox>
<bean:write property="couName" name="course"/>
</logic:iterate>
<html:submit></html:submit>
<html:reset></html:reset>
</html:form>
</body>
</html:html>








