SSH1框架整合,附图详解,简单登录程序(含源码) (申精)
废话不多说.直接开始1.建立数据库(MySQL)
程序代码:
/*
Navicat MySQL Data Transfer
Source Host : localhost:3306
Source Database : logindemo
Target Host : localhost:3306
Target Database : logindemo
Date: 2011-11-22 14:21:46
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`userName` varchar(50) default NULL,
`password` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'admin', 'admin');
INSERT INTO `user` VALUES ('2', 'guest', '');
Navicat MySQL Data Transfer
Source Host : localhost:3306
Source Database : logindemo
Target Host : localhost:3306
Target Database : logindemo
Date: 2011-11-22 14:21:46
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`userName` varchar(50) default NULL,
`password` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'admin', 'admin');
INSERT INTO `user` VALUES ('2', 'guest', '');
2.MyEclipse中配置数据库
进入后创建新链接
3.回到原工作窗口创建web工程
4.开始整合框架,我的添加顺序为:Spring,Hibernate,Struts1.2
添加Spring
包的列表如下: 2.5 AOP
2.5 Core
2.5 Persistence Core
2.5 Persistence JDBC
2.5 J2EE
2.5 WEB
Hibernate 3.2 Core
共七个类库(记住选择下面的 copy checked Library.....)把类库复制到/WebRoot/WEB-INF/lib
之后点下一步.点完成(默认选择)
然后在web.xml中配置Spring的启动配置.如下:
<!-- Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
添加Hibernate3.2
(看好选择的类库哦)
注意.这张图的.下面的单选.要选择下面的.把类库拷贝到WEB工程中...
下一步 (没必要截图了) 选择第二项 spring configuration file(applicationContext.xml)
再下一步(来张图了) 还是选择第二项 记得写sessionFactory
再下一步 配置数据源 填写 bean id: dataSource 再下拉列表选择第2步配置好的数据库即可
下一步.把复选框取消.然后直接点完成(不上图了) (出现包存在提示.点击覆盖)
以上的完成后.会有起冲突的包.找到如下图三个删掉
添加Struts1.2配置...这个简单.就一步(改下包的名字点击完成).如图
[ 本帖最后由 JeffLi 于 2011-11-22 16:21 编辑 ]
搜索更多相关主题的帖子:
localhost 建立数据库 structure
----------------解决方案--------------------------------------------------------
回复 楼主 JeffLi
开始写登录程序.持久层(Model)1.打开MyEclipse的数据库页面.(在右上角那选择).使用反向生成工具.如图:
2.做如下选择:
3.下一步. 主键增长类型选择 increment
4.下一步.默认.点完成.生成如下图目录
5.移动文件.UserDAO.java放到com.jeff.dao包中
6.右键点UserDAO.java -> Refactor -> Extract Interface 创建接口 选择要用到的方法名 如下图
7.移动文件...结果如下图:(发个全部包结构图.省得一会儿再创建).又后动创建了下面五个包
[ 本帖最后由 JeffLi 于 2011-11-22 15:59 编辑 ]
----------------解决方案--------------------------------------------------------
回复 2楼 JeffLi
开始写业务层(Control)1.创建业务层接口.在Iservice包中创建IUserService.java
程序代码:
package com.jeff.Iservice;
import com.jeff.domain.User;
public interface IUserService {
public void add(User user);
public User findByName(String username);
public void delete(User user);
}
2.创建业务层实现类.在service包中创建UserService.java 继承IUserServiceimport com.jeff.domain.User;
public interface IUserService {
public void add(User user);
public User findByName(String username);
public void delete(User user);
}
并加入UserDAO的私有变量.和set方法.(用Spring的DI)
程序代码:
package com.jeff.service;
import java.util.List;
import com.jeff.Iservice.IUserService;
import com.jeff.dao.UserDAO;
import com.jeff.domain.User;
public class UserService implements IUserService {
private UserDAO userDao;
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
public void add(User user) {
userDao.save(user);
}
public void delete(User user) {
userDao.delete(user);
}
public User findByName(String username) {
User user = null;
List<User> list = userDao.findByUserName(username);
if (list.size() > 0) {
user = list.get(0);
}
return user;
}
}
3.配置applicationContext.xml注入的userDao;import java.util.List;
import com.jeff.Iservice.IUserService;
import com.jeff.dao.UserDAO;
import com.jeff.domain.User;
public class UserService implements IUserService {
private UserDAO userDao;
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
public void add(User user) {
userDao.save(user);
}
public void delete(User user) {
userDao.delete(user);
}
public User findByName(String username) {
User user = null;
List<User> list = userDao.findByUserName(username);
if (list.size() > 0) {
user = list.get(0);
}
return user;
}
}
即加入如下代码:
程序代码:
<bean id="UserService" class="com.jeff.service.UserService">
<property name="userDao">
<ref bean="UserDAO" />
</property>
</bean>
<property name="userDao">
<ref bean="UserDAO" />
</property>
</bean>
业务层完成
[ 本帖最后由 JeffLi 于 2011-11-22 16:00 编辑 ]
----------------解决方案--------------------------------------------------------
回复 3楼 JeffLi
视图层(View)写代码挺麻烦的.咱接着开始生成
1.在工程中 文件目录 new -> Other -> 打开下图
下一步 记得添加两个属性username 和 password
下一步 改下最后的路径.点完成
2. 打开LoginAction.java文件.编写登录代码
程序代码:
package com.jeff.struts.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.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.jeff.domain.User;
import com.jeff.service.UserService;
import com.jeff.struts.form.LoginForm;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
ActionForward forward = mapping.getInputForward();
String userName = loginForm.getUsername();
String password = loginForm.getPassword();
WebApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(this.getServlet().getServletContext());
UserService userService = (UserService) ctx.getBean("UserService");
User user = userService.getUserByUserName(userName);
System.out.println(userName + password);
if (user != null) {
if (password.equals(user.getPassword())) {
forward = mapping.findForward("success");
} else {
forward = mapping.findForward("fail");
}
}
return forward;
}
}
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.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.jeff.domain.User;
import com.jeff.service.UserService;
import com.jeff.struts.form.LoginForm;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
ActionForward forward = mapping.getInputForward();
String userName = loginForm.getUsername();
String password = loginForm.getPassword();
WebApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(this.getServlet().getServletContext());
UserService userService = (UserService) ctx.getBean("UserService");
User user = userService.getUserByUserName(userName);
System.out.println(userName + password);
if (user != null) {
if (password.equals(user.getPassword())) {
forward = mapping.findForward("success");
} else {
forward = mapping.findForward("fail");
}
}
return forward;
}
}
3.修改struts-config.xml添加如下配置
程序代码:
<forward name="success" path="/welcome.jsp" />
<forward name="fail" path="/login.jsp" />
如图:<forward name="fail" path="/login.jsp" />
4.创建login.jsp
程序代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<form action="login.do">
用户名:
<input type="text" name="userName" value="lidong">
<br />
密 码:
<input type="password" name="password" value="123">
<br />
<input type="submit" value="登陆" />
</form>
</body>
</html>
5.创建welcome.jsp<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<form action="login.do">
用户名:
<input type="text" name="userName" value="lidong">
<br />
密 码:
<input type="password" name="password" value="123">
<br />
<input type="submit" value="登陆" />
</form>
</body>
</html>
程序代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'welcome.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<h1>
登陆成功
</h1>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'welcome.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<h1>
登陆成功
</h1>
</body>
</html>
6.布属发布.整合完成.测试下吧
成功了就试着把注册功能也完成吧.
----------------解决方案--------------------------------------------------------
我做的已上传到了论坛空间
http://down.bccn.net/1512
----------------解决方案--------------------------------------------------------
收录了。。。
----------------解决方案--------------------------------------------------------
顶起来
----------------解决方案--------------------------------------------------------