1.需求分析:
实现登陆并利用dtree实现显示菜单。其中,ssh框架的搭建,请参考博客(http://blog.csdn.net/vinsuan1993/article/details/68953986)。:
2.项目代码结构:
3.对于实体类的代码这里就不作过多的赘述,这里是通过MyEclipse从数据库反向生成实体类和xml文件(http://blog.csdn.net/qq_26222859/article/details/50521979,可以参考这份博客)。
userInfo.xml代码:
<?xml version="1.0"encoding="utf-8"?>
<!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 Persistence Tools
-->
<hibernate-mapping>
<class name="entity.Userinfo" table="USERINFO"schema="HNTESTCRM">
<id name="id" type="java.lang.Short">
<column name="ID" precision="3"scale="0" />
<generator class="sequence">
<param name="sequence">SEQ_USERINFO</param>
</generator>
</id>
<!-- lazy="false" 立即检索-->
<!-- 修改了fetch="join" -->
<many-to-one name="roleinfo"class="entity.Roleinfo" fetch="join"lazy="false">
<column name="ROLEID" precision="1"scale="0" not-null="true" />
</many-to-one>
<property name="username"type="java.lang.String">
<column name="USERNAME" length="20"not-null="true" />
</property>
<property name="userpwd" type="java.lang.String">
<column name="USERPWD" length="200"not-null="true" />
</property>
<property name="userflag"type="java.lang.Boolean">
<column name="USERFLAG" precision="1"scale="0" not-null="true" />
</property>
</class>
</hibernate-mapping>
RoleInfo.xml代码:
<?xml version="1.0"encoding="utf-8"?>
<!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 Persistence Tools
-->
<hibernate-mapping>
<class name="entity.Roleinfo" table="ROLEINFO"schema="HNTESTCRM">
<id name="id" type="java.lang.Boolean">
<column name="ID" precision="1"scale="0" />
<generatorclass="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="20"not-null="true" />
</property>
<property name="roledesc"type="java.lang.String">
<column name="ROLEDESC"length="100" />
</property>
<property name="roleflag"type="java.lang.Boolean">
<column name="ROLEFLAG" precision="1"scale="0" not-null="true" />
</property>
<set name="userinfos" inverse="true">
<key>
<columnname="ROLEID" precision="1" scale="0"not-null="true" />
</key>
<one-to-many class="entity.Userinfo" />
</set>
<!-- lazy="false" 立即检索-->
<!-- 修改了fetch="join" -->
<set name="rightinfos" table="ROLETORIGHT"schema="HNTESTCRM" lazy="false" fetch="join"sort="util.RightInfoIdComparator">
<key>
<columnname="ROLEID" precision="1" scale="0"not-null="true" />
</key>
<many-to-many entity-name="entity.Rightinfo">
<columnname="RIGHTID" precision="3" scale="0"not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
为了生成一条sql语句查询语句fetch="join",因为将其他xml代码没有修改之处略去。
4.Dao层实现类IUserInfoDAOImpl.java,
注意:1.内部类无法读取函数参数,但可以读取成员变量;
2.为了能读取userinfo,加上final;
3.对于一些复杂的查询,使用hibernate的回调函数HibernateCallback();
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
importorg.springframework.orm.hibernate3.HibernateCallback;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
import entity.Userinfo;
public class IUserinfoDAOImpl extendsHibernateDaoSupport implements
IUserinfoDAO{
privateString hql = null;
@Override
publicUserinfo findUserinfoByProperties(final Userinfo userinfo)
throwsException {
hql= "from Userinfo as u where u.username=? and u.userpwd=?";
returnthis.getHibernateTemplate().execute(new HibernateCallback(){
@Override
publicObject doInHibernate(Session session)
throws HibernateException,SQLException {
Queryquery = session.createQuery(hql);
query.setString(0,userinfo.getUsername());
query.setString(1,userinfo.getUserpwd());
returnquery.uniqueResult();
}
});
}
@Override
publicUserinfo get(Short id) throws Exception {
returnthis.getHibernateTemplate().get(Userinfo.class, id);
}
}
对应的spring-config-userinfo.xml(自动注解 default-autowire="byName",记的添加bean):
<?xml version="1.0"encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation=http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd default-autowire="byName">
<beanid="userinfoDAO"class="dao.IUserinfoDAOImpl"></bean>
<beanid="userinfoService" class="service.IUserinfoServiceImpl"></bean>
</beans>
Dao层测试类(断言):
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.FileSystemXmlApplicationContext;
import entity.Userinfo;
public class TestIUserinfoDAOImpl {
privateApplicationContext applicationContext;
privateUserinfo userinfo;
privateIUserinfoDAO dao;
@Before
publicvoid setUp() throws Exception {
applicationContext= newFileSystemXmlApplicationContext("classpath:spring-config-*.xml");
dao= (IUserinfoDAO) applicationContext.getBean("userinfoDAO");
userinfo= new Userinfo();
userinfo.setUsername("admin");
userinfo.setUserpwd("admin");
}
@After
publicvoid tearDown() throws Exception {
applicationContext= null;
}
@Test
publicvoid testFindUserinfoByProperties() {
try{
userinfo= dao.findUserinfoByProperties(userinfo);
Assert.assertNotNull(userinfo);
}catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
publicvoid testGet() {
try{
userinfo= dao.get(new Short("2"));
Assert.assertNotNull(userinfo);
}catch (NumberFormatException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.service层实现类(1.登陆用户查询2.创建菜单(将menu写到页面),这里使用的是dtree生成js,dtree资源
下载:http://download.csdn.net/detail/vinsuan1993/9802366):
import dao.IUserinfoDAO;
import entity.Rightinfo;
import entity.Userinfo;
public class IUserinfoServiceImplimplements IUserinfoService {
privateIUserinfoDAO userinfoDAO;
publicvoid setUserinfoDAO(IUserinfoDAO userinfoDAO) {
this.userinfoDAO= userinfoDAO;
}
@Override
publicUserinfo findUserinfoByProperties(Userinfo userinfo)
throwsException {
returnuserinfoDAO.findUserinfoByProperties(userinfo);
}
@Override
publicString createMenu(Short id) throws Exception {
Userinfouserinfo = userinfoDAO.get(id);
Set<Rightinfo>rightinfos = userinfo.getRoleinfo().getRightinfos();
StringBuffermenu = new StringBuffer("<table><tr><divclass='dtree'><script type='text/javascript'>");
menu.append("crm=newdTree('crm');");
for(Rightinfor:rightinfos){
menu.append("crm.add(")
.append(r.getId()).append(",")
.append(r.getPid()).append(",'")
.append(r.getName()).append("','")
.append(r.getUrl()).append("','")
.append(r.getTitle()).append("','")
.append(r.getTarget()).append("');");
}
menu.append("document.write(crm);crm.openAll();</script></div></tr></table>");
returnmenu.toString();
}
}
6.login.jsp代码:
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s"uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<base href="<%=basePath%>">
<TITLE>请登录</TITLE>
<METAHTTP-EQUIV="Content-Type" CONTENT="text/html;charset=GB2312">
<style>
th{
font-size:12px;
text-align:right;
font-weight:normal;
}
td{
font-size:12px;
text-align:left;
}
input{
width:100px;
font-size:12px;
border:solid1px lightblue;
}
</style>
</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0style="text-align:center;padding-top:20px;">
<TABLE WIDTH=800 BORDER=0 CELLPADDING=0CELLSPACING=0>
<TR>
<TDCOLSPAN=7>
<IMGSRC="images/login/login_01.jpg" WIDTH=800 HEIGHT=71ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=71ALT=""></TD>
</TR>
<TR>
<TDCOLSPAN=7>
</TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=66ALT=""></TD>
</TR>
<TR>
<TDCOLSPAN=7>
<IMGSRC="images/login/login_03.jpg" WIDTH=800 HEIGHT=6ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=6ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=3> </TD>
<TDCOLSPAN=3 ROWSPAN=2>
<IMGSRC="images/login/login_05.jpg" WIDTH=426 HEIGHT=83ALT=""></TD>
<TD ROWSPAN=3> </TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=44ALT=""></TD>
</TR>
<TR>
<TD ROWSPAN=4> </TD>
<TDCOLSPAN=2>
<IMGSRC="images/login/login_08.jpg" WIDTH=94 HEIGHT=39ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=39ALT=""></TD>
</TR>
<TR>
<TDROWSPAN=3>
<IMGSRC="images/login/login_09.jpg" WIDTH=15 HEIGHT=141ALT=""></TD>
<TD COLSPAN=2 ROWSPAN=2background="images/login/login_10.jpg" >
<s:form action="userinfo/userinfoAction!login.action"method="post" name="loginForm">
<tablewidth="100%">
<tr><tdcolspan="2"><fontcolor="red"><s:actionerror/></font></td></tr>
<tr>
<th>用户名</th>
<td><s:textfield name="userinfo.username"size="10" maxlength="20"/></td>
</tr>
<tr>
<th>密码</th>
<td><s:password name="userinfo.userpwd"size="10" maxlength="20"/></td>
</tr>
<tr>
<td> </td>
<td><img onclick="document.loginForm.submit();"src="images/login/login_button.jpg" width="73"height="25"></td>
</tr>
</table>
</s:form>
</TD>
<TDCOLSPAN=2>
<IMGSRC="images/login/login_11.jpg" WIDTH=304 HEIGHT=86ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=86ALT=""></TD>
</TR>
<TR>
<TDROWSPAN=2>
<IMGSRC="images/login/login_12.jpg" WIDTH=19 HEIGHT=55ALT=""></TD>
<TD COLSPAN=2 ROWSPAN=3> </TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=28ALT=""></TD>
</TR>
<TR>
<TDCOLSPAN=2>
<IMGSRC="images/login/login_14.jpg" WIDTH=201 HEIGHT=27ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=27ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=5> </TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=67ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=7 background="images/login/login_16.jpg"style="text-align:right;padding-right:25px;">
©2008-2010 REVEL CORP. </TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=1 HEIGHT=55ALT=""></TD>
</TR>
<TR>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=150 HEIGHT=1ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=15 HEIGHT=1ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=79 HEIGHT=1ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=122 HEIGHT=1ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=19 HEIGHT=1ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=285 HEIGHT=1ALT=""></TD>
<TD>
<IMGSRC="images/login/spacer.gif" WIDTH=130 HEIGHT=1ALT=""></TD>
<TD></TD>
</TR>
</TABLE>
</BODY>
</HTML>
6.action层代码示例:
importorg.apache.struts2.ServletActionContext;
import service.IUserinfoService;
importcom.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import entity.Userinfo;
/**
* 用户信息的控制器类
*@author Administrator
*
*/
public class UserinfoAction extendsActionSupport {
privateUserinfo userinfo;
privateIUserinfoService userinfoService;
publicUserinfo getUserinfo() {
returnuserinfo;
}
publicvoid setUserinfo(Userinfo userinfo) {
this.userinfo= userinfo;
}
publicvoid setUserinfoService(IUserinfoService userinfoService) {
this.userinfoService= userinfoService;
}
/**
* 登录
* @return
*/
publicString login(){
Stringresult="success";
try{
userinfo= userinfoService.findUserinfoByProperties(userinfo);
if(userinfo==null){
this.addActionError("用户名或密码错误");
result= "input";
}else{
Stringmenu = userinfoService.createMenu(userinfo.getId());
ServletActionContext.getRequest().getSession().setAttribute("username",userinfo.getUsername());
ServletActionContext.getRequest().getSession().setAttribute("menu",menu);
}
}catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returnresult;
}
}
7.框架集页面结构:
Main.jsp代码:
<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>客户关系管理系统</title>
<metahttp-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<frameset rows="85,*,40"frameborder="NO" noresize Borders="NO"framespacing="0">
<frame name="topFrame"frameborder="NO" scrolling="NO" noresizeBorders="NO" src="userinfo/top.jsp" marginwidth="value"marginheight="value" >
<frameset cols="200,*"frameborder="1" Borders="0" framespacing="0">
<frame name="leftFrame" Borders="0"src="userinfo/left.jsp" marginwidth="value"marginheight="value" >
<frame name="main" Borders="0"src="userinfo/center.jsp" marginwidth="value"marginheight="value" >
</frameset>
<frame src="userinfo/footer.html"name="footer" frameborder="NO" scrolling="NO"noresize marginwidth="0"marginheight="0" Borders="NO" >
</frameset>
<noframes><bodybgcolor="#FFFFFF">
</body></noframes>
</html>
Top.jsp代码:
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>客户关系管理系统</title>
<metahttp-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body style="border-bottom:solid1px #666;">
<TABLE style="width:100%;">
<TR >
<td><img src="images/logo.gif"></td>
<tdstyle="font-family:黑体;font-size:33px;font-weight:bold;"> 客户关系管理系统</td>
<tdwidth="25%" align="right" style="font-size:12px;"valign="bottom">当前用户:${username} 退出系统 </td>
</tr>
</table>
</body>
</html>
Left.jsp代码:
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>客户关系管理系统</title>
<linkhref="dtree/dtree.css" type="text/css"rel="stylesheet">
<scriptsrc="dtree/dtree.js"type="text/javascript"></script>
</head>
<body>
${menu}
</body>
</html>
Footer.jsp代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>欢迎使用!</title>
<linkrel="stylesheet" type="text/css"href="styles.css" >
</head>
<bodystyle="font-size:12px;color:dark-blue;scroll:no;border-top:solid 1px#666;">
<divstyle="text-align:right;padding:6px;">
© 2008-2012 REVEL CORP.
</div>
</body>
</html>
Center.jsp代码:
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>welcome</title>
<metahttp-equiv="Content-Type" content="text/html;charset=gb2312">
<link href="css/style.css" rel="stylesheet"type="text/css">
</head>
<body>
<imgsrc="images/welcome.gif" />
</body>
</html>
本文介绍了一个SSH框架实现登录功能的实例,并结合dtree展示菜单。首先进行需求分析,然后展示了项目的代码结构,包括实体类、XML配置、DAO层、Service层和Action层的代码细节。此外,还提供了login.jsp页面和框架集页面结构的组成部分,如Main.jsp、Top.jsp、Left.jsp和Footer.jsp的代码示例。
1485

被折叠的 条评论
为什么被折叠?



