myeclipse自身携带了很多软件开发包,象J2EE,jstl,struts,spring,hibernate,jsf,这些软件开发包单独可以使用,能够展现自身完美功能,但是单独使用一方开发的构架,则不能使软件开发过程变的更加简单,如果要综合使用多中技术架构,包冲突也就不可避免了。解决包冲突,其实也很简单,要删除一些包,或者增加一些包。myeclipse将常用的java软件组装起来,给开发者带来了很多的便利。
myeclipse7.0只支持struts1的集成,struts1可能是第一个J2EE MVC构架,其结构简单,使用简单,稳定性好。使用它开发的团体很多。struts2不是直接继承strut1,而是继承了J2EE的另一个架构Webwork。如果想使用struts2,需要从www.apache.org网站了下载。
spring2.5和hibernate3.2已经包含在了myeclipse里面,因此就不用下载。hibernate是数据库持久操作,需要选择使用的数据库以及次数据库的JDBC。由于mySQL软件较小,在各种平台上都有其实现,也就是可移植性好。
建立数据库,create database example;
选择数据库 use example;
建立数据库表,创建一个用户登入表
create table user (
id int unsigned not null auto_increment,
username varchar(50),
password varchar(50),
primary key(id)
) ;
插入几条数据记录
insert into user (username, password) values ('dog','dog');
insert into user (username, password) values ('cat','cat');
insert into user (username, password) values ('tiger','tiger');
insert into user (username, password) values ('pig','pig');
新建web工程,名字为example,选择J2EE 5.0
新建几个包,com.model, com.manager,com.action
包加入:
hibernate操作,myeclipse 支持两种方法建设映射,一种配置文件,一种是注解法,二种方法都很流行,性能都不错。
使用配置房间的方法配置文件
加入hibernate capability 选择hibernate 3.2,去掉hibernate3.2 Annotation&Entity manager,选择Copy checked library jars to project folder and bulid path,去掉Create SessionFactory class选项,然后按finish
建立映射, 选择MyEclipse Hibernate视图,选择native生成方式
spring操作
加入spring capability 选择spring2.5,在选择Aop Libraries, Core Libraries, Persistence Core Libraries,Persistence JDBC Libraries,Web Libraries,选择Copy checked library jars to project folder and bulid path,然后按finishe
struts2操作
下载好struts2以后,将struts2中的包加到工程下面,*.plugin都不能要,除了struts2
业务操作
UserManager.java
package com.manager;
import java.util.List;
import com.model.User;
public interface UserManager {
List list();
User get(String username, String password);
}
UserDao.java
package com.manager;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.model.User;
public class UserDao extends HibernateDaoSupport implements UserManager {
public User get(String username, String password) {
String queryString = "from User as u where u.username='" + username;
queryString += "' and u.password='" + password + "'";
List list = this.getHibernateTemplate().find(queryString);
if (list.size() > 0) {
return (User) list.get(0);
} else
return null;
}
public List list() {
// TODO Auto-generated method stub
String queryString = "from User";
return this.getHibernateTemplate().find(queryString);
}
}
LoginAction.java
package com.action;
import java.util.List;
import com.manager.UserManager;
import com.model.User;
public class LoginAction {
private UserManager userManager;
private User u;
private String username;
private String password;
private List userList;
public List getUserList() {
return userList;
}
public void setUserList(List userList) {
this.userList = userList;
}
public UserManager getUserManager() {
return userManager;
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() {
if (username == null && username.length() == 0)
return "error";
if (password == null && password.length() == 0)
return "error";
u = userManager.get(username, password);
if (u == null)
return "error";
else {
userList=(List)userManager.list();
return "success";
}
}
}
配置文件
在web.xml里面加入:
<context-param>
<param-name>contextConfigLoaction</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listen-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<filter-mapping>
struts.xml
<struts>
<include file="struts-default.xml">
<constant name="struts-objectFactory" value="spring">
<package name="example" extends="struts-default">
<action name="loginAction" class="loginAction">
<result name="success">success.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
</struts>
applicationContext.xml加入
<bean id="userManager" class="com.manager.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="loginAction" class="com.action.LoginAction">
<property name="userManager" ref="userManager"></property>
</bean>
页面代码
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:form action ="loginAction">
<s:textfield name="username"> </s:textfield>
<s:textfield name="password"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:iterator value="userList">
<s:property value="username"/>
<s:property value="password"/>
<br>
</s:iterator>
</body>
</html>
去掉lib的asm-2.2.3.jar
启动tomcat
再浏览器中输入:http://localhost:8080/example
进行测试。