一、 新建web项目
1. File->New->web project
2. 工程名称:king-back->点选Java EE 6.0标准
二、 添加ssh2支持
1. 添加struts2
工程上点右键->myeclipse->add struts capabilites
点选struts2.1->url模式点选 /*
添加struts2 core lib和struts2 spring lib
新建LoginAction类
package com.frank.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author steve frank
*
*/
public class LoginAction extends ActionSupport {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() {
if (name.equals("steve") && password.equals("123"))
return SUCCESS;
else
return INPUT;
}
}
修改struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.frank.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="input">/fail.jsp</result>
</action>
</package>
</struts>
修改index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<s:form action="login">
<s:textfield name="name" value="steve"></s:textfield>
<s:textfield name="password" value="123"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
添加success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'success.jsp' starting page</title>
</head>
<body>
<s:text name="password" />
<hr />
<s:property value="password" />
<hr />
${password}
<hr />
</body>
</html>
添加fail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'fail.jsp' starting page</title>
</head>
<body>登录失败,用户名或密码错误.
</body>
</html>
当前web.xml如下,直接启动会报spring监听错误,因为刚才已经添加了spring支持struts的lib包。
添加spring监听,重新启动会提示找不到spring配置文件。
2. 添加spring
右键->myeclipse->add spring capabilities
勾选spring 3.0 aop lib,spring 3.0 core lib,spring 3.0 persistence core lib,spring 3.0 persistence jdbc lib,spring 3.0 web lib;
点选copy checked library contents to project folder
当前web.xml如下,重新启动tomcat,成功。
打开浏览器,输入http://localhost:8088/king-back,(tomcat端口未修改过的应该为8080),出现如下界面。
直接点击submit,成功跳转到success.jsp,界面如下。
如果修改用户名为frank,如下
点击submit后跳转到fail.jsp如下
3. 添加hibernate
先配置好数据源驱动,如下
右键->myeclipse->add hibernate capabilities
点选hibernate 3.3,勾选enable hibernate annotations support;
勾选hibernate 3.3 annotations&entity manager,hibernate 3.3 core lib;
点选copy checked library jars to project folder and add to build-path
点选spring configuration file,交给spring配置数据源
勾选existing spring configuration file
选择配置好的数据源驱动
去掉create seesionfactory class的勾。然后点击完成。
三、 数据反向生成
1. 新建文件夹com.frank.model,准备放置生成的实体类和dao类
2. 在项目中新建data.sql文件,内容如下,连接mysql的test库,新建表student
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(20) default NULL,
`role_id` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '张三', '1');
INSERT INTO `student` VALUES ('2', '李四', '2');
INSERT INTO `student` VALUES ('3', '王五', '1');
SELECT * FROM student;
3. 生成Dao和实体类过程如下
打开Test库,选择student表,右键hibernate reverse engineering
出现如下视图,通过browse选择king-back项目和com.frank.model文件夹
结果如下,
勾选create pojo<> db table mapping information->点选add hibernate mapping annotations to pojo->保留update hibernate configuration with mapping resource location;
去掉create abstract class勾;
勾选java data access object(dao) ->勾选generate precise findby methods->点选spring dao
Id generator 选择native
结果如下
四、 编写测试
1. 新建test类
package test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.frank.model.Student;
import com.frank.model.StudentDAO;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
StudentDAO dao = (StudentDAO) context.getBean("StudentDAO");
List<Student> list = dao.findAll();
int n = list.size();
for (int i = 0; i < n; i++) {
Student c = list.get(i);
String name = c.getName();
System.out.println(name);
}
}
}
运行结果如下,说明项目基本上是ok的。
2. 修改action类
public String execute() {
getData();
if (name.equals("steve") && password.equals("123"))
return SUCCESS;
else
return INPUT;
}
private void getData() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
StudentDAO dao = (StudentDAO) context.getBean("StudentDAO");
List<Student> list = dao.findAll();
int n = list.size();
for (int i = 0; i < n; i++) {
Student c = list.get(i);
String name = c.getName();
System.out.println(name);
}
}
刷新浏览器,查看控制台打印结果。