《JAVA EE企业应用开发》综合性、设计性实验成绩单开设时间:2012学年第一学期班级10信管1班学号 1.2010302601332.2010305601313.201030560135 姓名 1.张伟东2.吴雨瑞3.郑易东实验题目实验一开发struts2程序实验成绩教师签名《JAVA EE企业应用开发》实验报告实验题目:开发struts2程序实验指导教师:杨春实验组长(姓名+学号):张伟东201030560133组员(姓名+学号):张伟东201030560133吴雨瑞201030560131郑易东201030560135实验时间:2012年11月组长签名:(手写)2012年12 月 2 日1、实验目的(1分)1).掌握用不同框架模式开发JAVA EE程序;2).使用JSP和不同框架混合模式编写一个简单的用户信息管理系统(具体描述见实验步骤部分)2、实验内容(2分)考虑实现一个数据库单表操作的简单JAVA EE WEB应用,使用struts2 框架框架中的实现.系统需求如下:普通用户功能:用户登录系统;用户注册;修改个人信息;登录后查看系统资源。
系统管理员:全部用户列表;根据姓名模糊查询符合条件的用户或用户列表;删除用户。
3、实验步骤与结果(3分)主要开发步骤如下:1)数据可设计,生成数据库;2)创建web project,名为xs;3)功能模块包划分;4)搭建系统框架;5)普通用户功能实现:注册、登录、修改个人信息等;6)系统管理员功能实现:查询、删除等。
实现结果,截图如下:数据库:Jsp界面(部分):4、架构设计+源码(2分)架构设计如下图:源码(主要):Guanli.actionpackage action;import java.util.Map;import service.IGuanliService;import service.impl.GuanliService;import vo.Guanli;import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class GuanliAction extends ActionSupport{protected Guanli guanli;protected IGuanliService guanliService;protected String username;protected String password;public Guanli getGuanli() {return guanli;}public void setGuanli(Guanli guanli) {this.guanli = guanli;}public IGuanliService getGuanliService() {return guanliService;}public void setGuanliService(IGuanliService guanliService) {this.guanliService = guanliService;}public String getUsername() {return username;}public void setUsername(String username) {ername = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String execute() throws Exception{Guanlig=guanliService.validateGuanli(guanli.getUsername(),guanli.getPassword());if(g!=null){Map session=ActionContext.getContext().getSession();session.put("guanli", g);return SUCCESS;}else{return ERROR;}}}Zhuce.actionpackage action;import java.util.Map;import service.IZhuceService;import vo.Guanli;import vo.Zhuce;import java.util.List;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class ZhuceAction extends ActionSupport{protected Zhuce zhuce;protected IZhuceService zhuceService;private String username;private String password;private String sex;private String address;public Zhuce getZhuce() {return zhuce;}public void setZhuce(Zhuce zhuce) {this.zhuce = zhuce;}public IZhuceService getZhuceService() {return zhuceService;}public void setZhuceService(IZhuceService zhuceService) {this.zhuceService = zhuceService;}public String getUsername() {return username;}public void setUsername(String username) {ername = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String execute() throws Exception{Zhucez=zhuceService.validateZhuce(zhuce.getUsername(),zhuce.getPassword());List zcs=zhuceService.getZhuce(zhuce.getUsername());Map request=(Map)ActionContext.getContext().get("request");request.put("zcs", zcs);if(z!=null){Map session=ActionContext.getContext().getSession();session.put("zhuce", z);return SUCCESS;}else{return ERROR;}}public String zc() throws Exception{zhuceService.saveZhuce(zhuce);return SUCCESS;}public String grxx() throws Exception{List zcs=zhuceService.getZhuce(username);Map request=(Map)ActionContext.getContext().get("request");request.put("zcs", zcs);return SUCCESS;}public String sc() throws Exception{zhuceService.dl(zhuce.getUsername());List zcs=zhuceService.getZhuce1();Map request=(Map)ActionContext.getContext().get("request");request.put("zcs", zcs);return SUCCESS;}public String xg() throws Exception{zhuceService.xiugai(zhuce.getUsername(),zhuce.getPassword(),zhuce.getSex(),zhuce.get Address());List zcs=zhuceService.getZhuce(zhuce.getUsername());Map request=(Map)ActionContext.getContext().get("request");request.put("zcs", zcs);return SUCCESS;}public String cz() throws Exception{List zcs=zhuceService.cz(zhuce.getUsername());Map request=(Map)ActionContext.getContext().get("request");request.put("zcs", zcs);return SUCCESS;}}GuanliDaopackage dao.impl;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.List;import dao.BaseDAO;import dao.IGuanliDAO;import vo.Guanli;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;public class GuanliDAO extends BaseDAO implements IGuanliDAO{public Guanli validateGuanli(String username,String password){Session session=getSession();String hql="from Guanli g where ername=? and g.password=?";Query query=session.createQuery(hql);query.setParameter(0,username);query.setParameter(1, password);List guanlis=query.list();if(guanlis.size()!=0){Guanli guanli=(Guanli)guanlis.get(0);return guanli;}session.close();return null;}}ZhuceDaopackage dao.impl;import java.util.List;import dao.BaseDAO;import dao.IZhuceDAO;import vo.Zhuce;import org.hibernate.Query;import org.hibernate.classic.Session;import org.hibernate.Transaction;public class ZhuceDAO extends BaseDAO implements IZhuceDAO{public void saveZhuce(Zhuce zhuce){Session session=getSession();Transaction tx=session.beginTransaction();session.save(zhuce);mit();session.close();}public List getZhuce(String username){Session session=getSession();Query query=session.createQuery("from Zhuce z where ername=?");query.setParameter(0, username);List zcs=query.list();session.close();return zcs;}public List getZhuce1(){Session session=getSession();Query query=session.createQuery("from Zhuce z");List zcs=query.list();session.close();return zcs;}public void dl(String username){Session session=getSession();Query query=session.createQuery("delete Zhuce z where ername=?");query.setParameter(0, username);query.executeUpdate();}public void xiugai(String username,String password,String sex,String address){ Session session=getSession();Query query=session.createQuery("update Zhuce z setername=?,z.password=?,z.sex=?,z.address=? where ername=?");query.setParameter(0, username);query.setParameter(1, password);query.setParameter(2, sex);query.setParameter(3, address);query.setParameter(4, username);query.executeUpdate();}public Zhuce validateZhuce(String username,String password){Session session=getSession();String hql="from Zhuce z where ername=? and z.password=?";Query query=session.createQuery(hql);query.setParameter(0,username);query.setParameter(1, password);List zhuces=query.list();if(zhuces.size()!=0){Zhuce zhuce=(Zhuce)zhuces.get(0);return zhuce;}session.close();return null;}public List cz(String username){Session session=getSession();Query query=session.createQuery("from Zhuce z where ername=?");query.setParameter(0, username);List zcs=query.list();session.close();return zcs;}}Struts.xml<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "/dtds/struts-2.1.dtd"><struts><include file="struts-default.xml"/><package name="default" extends="struts-default"><action name="zc" class="zhuceAction" method="zc"><result name="success">/success.jsp</result></action><action name="grxx" class="zhuceAction" method="grxx"><result name="success">/grxx.jsp</result></action><action name="cz" class="zhuceAction" method="cz"><result name="success">/cz.jsp</result></action><action name="sc" class="zhuceAction" method="sc"><result name="success">/sc.jsp</result></action><action name="xg" class="zhuceAction" method="xg"><result name="success">/xg.jsp</result></action><action name="dl2" class="guanliAction"><result name="success">/dl2s.jsp</result><result name="error">/dl2.jsp</result></action><action name="dl" class="zhuceAction"><result name="success">/dls.jsp</result><result name="error">/dl.jsp</result></action></package></struts>applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:p="/schema/p"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans-3.0.xsd"><bean id="dataSource"class="mons.dbcp.BasicDataSource"><property name="driverClassName"value="com.mysql.jdbc.Driver"></property><property name="url"value="jdbc:mysql://localhost:3306/xs"></property><property name="username" value="root"></property><property name="password" value="123"></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>vo/Zhuce.hbm.xml</value><value>vo/Guanli.hbm.xml</value></list></property></bean><bean id="baseDAO" class="dao.BaseDAO" abstract="true"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean><bean id="zhuceDAO" class="dao.impl.ZhuceDAO" parent="baseDAO"></bean><bean id="zhuceService" class="service.impl.ZhuceService"><property name="zhuceDAO"><ref bean="zhuceDAO"/></property></bean><bean id="zhuceAction" class="action.ZhuceAction"><property name="zhuceService"><ref bean="zhuceService"/></property></bean><bean id="guanliDAO" class="dao.impl.GuanliDAO" parent="baseDAO"> </bean><bean id="guanliService" class="service.impl.GuanliService"><property name="guanliDAO"><ref bean="guanliDAO"/></property></bean><bean id="guanliAction" class="action.GuanliAction"><property name="guanliService"><ref bean="guanliService"/></property></bean></beans>Dl.jsp(登录)<%@ page contentType="text/html;charset=utf-8" %><html><body><form action="dl.action" method="post">用户名:<input type="text" name="ername"/><br>密码:<input type="password" name="zhuce.password"/><br> <input type="submit" value="登录"></form></body></html>Grxx.jsp(显示会员信息)<%@ page contentType="text/html;charset=utf-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head></head><body><s:iterator value="#request['zcs']" id="zhuce"><s:property value="#ername"/><s:property value="#zhuce.password"/><s:property value="#zhuce.sex"/><s:property value="#zhuce.address"/></s:iterator><a href="dls.jsp">返回</a></body></html>Zhuce.jsp(注册)<%@ page contentType="text/html;charset=utf-8" %><HTML> <BODY><table align="center"><tr><td height=50></td></tr><tr><td><form action="zc.action" method="post">用户名:<br><input type="text" name="ername"/><p>密码:<br><input type="password" name="zhuce.password"/><p>性别:<br><input type="text" name="zhuce.sex"/><p>地址:<br><input type="text" name="zhuce.address"/><p><input type="submit" value="注册" ></form> </td></tr><tr><td><a href="d.jsp">登录</a></td></tr></table></BODY></HTML>5、结论与讨论(2分)通过这次实验,我们小组成员认识到应用框架的方便之处和它的强大之处,虽然只是应用了其中的一小部分,但为接下去的学习打下基础。