`
lijunaccp
  • 浏览: 152835 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ssh2

阅读更多
准备条件:
1. 建WEB工程:ssh2
2. 建数据库ssh2,表user
3. 导入struts2,hibernate,spring包,注意包冲突,另导入commons-dbcp.jar, commons-pool.jar数据库连接池包
具体步骤如下:
1. 实体类
1.1 User.java
package com.ssh2.bean;

public class User {
	
	private int id;
	private String firstname;
	private String lastname;
	private int age;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

2. 数据库层
2.1 UserDAO.java
package com.ssh2.dao;

import java.util.List;

import com.ssh2.bean.User;

public interface UserDAO {
	public void saveUser(User user);
	public void updateUser(User user);
	public void deleteUser(User user);
	public List<User> getAllUsers();
	public User getUser(Integer id);
}

2.2 UserDAOImpl.java
package com.ssh2.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ssh2.bean.User;
import com.ssh2.dao.UserDAO;

public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

	public void saveUser(User user) {
		this.getHibernateTemplate().save(user);
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getAllUsers() {
		List<User> users=(List<User>)this.getHibernateTemplate().find("from User");
		return users;
	}
	
	public User getUser(Integer id) {
		return (User)this.getHibernateTemplate().get(User.class, id);
	}
	
	public void updateUser(User user) {
		this.getHibernateTemplate().saveOrUpdate(user);
	}
	
	public void deleteUser(User user) {
		this.getHibernateTemplate().delete(user);
	}
}

3. 业务逻辑层
3.1 UserService.java
package com.ssh2.service;

import java.util.List;

import com.ssh2.bean.User;

public interface UserService {
	public void addUser(User user);
	public void updateUser(User user);
	public void deleteUser(User user);
	public List<User> getAllUsers();
	public User getUser(Integer id);
}

3.2 UserServiceImpl.java
package com.ssh2.service.impl;

import java.util.List;

import com.ssh2.bean.User;
import com.ssh2.dao.UserDAO;
import com.ssh2.service.UserService;

public class UserServiceImpl implements UserService {
	
	private UserDAO userDAO;
	
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void addUser(User user) {
		userDAO.saveUser(user);
	}
	
	public List<User> getAllUsers() {
		return userDAO.getAllUsers();
	}

	public User getUser(Integer id) {
		return userDAO.getUser(id);
	}
	
	public void updateUser(User user) {
		userDAO.updateUser(user);
	}
	
	public void deleteUser(User user) {
		userDAO.deleteUser(user);
	}
}

4. 表示层
4.1  AddUserAction.java
package com.ssh2.action.user;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh2.bean.User;
import com.ssh2.service.UserService;

@SuppressWarnings("serial")
public class AddUserAction extends ActionSupport {
	
	private User user;
	private UserService userService;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		userService.addUser(user);
		return SUCCESS;
	}
}

4.2 DeleteUserAction.java
package com.ssh2.action.user;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh2.bean.User;
import com.ssh2.service.UserService;

@SuppressWarnings("serial")
public class DeleteUserAction extends ActionSupport {
	private User user;
	private UserService userService;
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@Override
	public String execute() throws Exception {
		userService.deleteUser(user);
		return SUCCESS;
	}
}

4.3 ListUserAction.java
package com.ssh2.action.user;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh2.bean.User;
import com.ssh2.service.UserService;

@SuppressWarnings("serial")
public class ListUserAction extends ActionSupport {

	private UserService userService;
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		List<User> users=userService.getAllUsers();
	    Map request=(Map)ActionContext.getContext().get("request");
	    request.put("users", users);
		return SUCCESS;
	}
}

4.4 UpdateAction.java
package com.ssh2.action.user;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh2.bean.User;
import com.ssh2.service.UserService;

@SuppressWarnings("serial")
public class UpdateAction extends ActionSupport {
	private User user;
	private UserService userService;
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@Override
	public String execute() throws Exception {
		userService.updateUser(user);
		return SUCCESS;
	}
}

4.5 UpdatePAction.java
package com.ssh2.action.user;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh2.bean.User;
import com.ssh2.service.UserService;

public class UpdatePAction extends ActionSupport {
	private User user;
	private UserService userService;
	
	public void setUser(User user) {
		this.user = user;
	}
	
	public User getUser() {
		return user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		Map map=(Map)ActionContext.getContext().get("request");
		map.put("user", this.userService.getUser(user.getId()));
		return SUCCESS;
	}
}

5. 配置文件
5.1 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="ssh2" extends="struts-default">
		<action name="addUser" class="addUserAction">
			<result name="success" type="redirect">listUser.action</result>
		</action>
		<action name="listUser" class="listUserAction">
			<result name="success">/list.jsp</result>
		</action>
		<action name="updateP" class="updatePAction">
			<result name="success">/update.jsp</result>
		</action>
		<action name="update" class="updateAction">
			<result name="success" type="redirect">listUser.action</result>
		</action>
		<action name="deleteUser" class="deleteUserAction">
			<result name="success" type="redirect">listUser.action</result>
		</action>
		
	</package>
</struts>

5.2 applicationContext.xml(放到WEB-INF下)
<?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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/ssh2</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>root</value>
		</property>
	</bean>
	
	<bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="ds"></property>
		<property name="mappingResources">
			<list>
				<value>com/ssh2/bean/User.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>
	
    <bean id="userDAO" class="com.ssh2.dao.impl.UserDAOImpl">
    	<property name="sessionFactory" ref="sf"></property>
    </bean>
    
    <bean id="userService" class="com.ssh2.service.impl.UserServiceImpl">
    	<property name="userDAO" ref="userDAO"></property>
    </bean>
    
    <bean id="addUserAction" class="com.ssh2.action.user.AddUserAction">
    	<property name="userService" ref="userService"></property>
    </bean>
    
    <bean id="listUserAction" class="com.ssh2.action.user.ListUserAction">
    	<property name="userService" ref="userService"></property>
    </bean>
    
    <bean id="updatePAction" class="com.ssh2.action.user.UpdatePAction">
    	<property name="userService" ref="userService"></property>
    </bean>
    
    <bean id="updateAction" class="com.ssh2.action.user.UpdateAction">
    	<property name="userService" ref="userService"></property>
    </bean>
    
    <bean id="deleteUserAction" class="com.ssh2.action.user.DeleteUserAction">
    	<property name="userService" ref="userService"></property>
    </bean>
</beans>

6. 映射文件
5.1 User.hbm.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" >
<hibernate-mapping>
	<class name="com.ssh2.bean.User" table="user">
		<id name="id" column="id" type="integer">
			<generator class="increment"></generator>
		</id>
		<property name="firstname" column="firstname" type="string"></property>
		<property name="lastname" column="lastname" type="string"></property>
		<property name="age" column="age" type="integer"></property>
	</class>
</hibernate-mapping>

7. 页面
7.1 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>首页</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <s:a href="add.jsp">增加用户</s:a><br/>
    <s:a href="listUser.action">用户列表</s:a>
  </body>
</html>

7.2 list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
</head>
<body>
	<table border="1" cellpadding="1" cellspacing="0" align="center" width="60%">
		<tr>
			<th>id</th>
			<th>firstname</th>
			<th>lastname</th>
			<th>age</th>
			<th>update</th>
			<th>delete</th>
		</tr>
		<s:iterator value="#request.users" id="user">
			<tr>
				<td><s:property value="#user.id" /></td>
				<td><s:property value="#user.firstname" /></td>
				<td><s:property value="#user.lastname" /></td>
				<td><s:property value="#user.age" /></td>
				<td><s:a href="updateP.action?user.id=%{#user.id}">update</s:a></td>
				<td><s:a href="deleteUser.action?user.id=%{#user.id}">delete</s:a></td>
			</tr>
		</s:iterator>
	</table>
</body>
</html>

7.3 add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册用户</title>
</head>
<body>
	<s:form action="addUser">
		<s:textfield name="user.firstname" label="firstname"></s:textfield>
		<s:textfield name="user.lastname" label="lastname"></s:textfield>
		<s:textfield name="user.age" label="age"></s:textfield>
		<s:submit value="submit"></s:submit>
	</s:form>
</body>
</html>

7.4 update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>更新用户</title>
</head>
<body>
	<s:form action="update.action">
		<s:hidden name="user.id" value="%{#request.user.id}" ></s:hidden>
		<s:textfield name="user.firstname" value="%{#request.user.firstname}" label="firstname"></s:textfield>
		<s:textfield name="user.lastname" value="%{#request.user.lastname}" label="lastname"></s:textfield>
		<s:textfield name="user.age" value="%{#request.user.age}" label="age"></s:textfield>
		<s:submit value="submit"></s:submit>
	</s:form>
</body>
</html>
分享到:
评论
9 楼 stuxnet 2013-01-23  
struts 内action间的跳转要加"redirect-action"
8 楼 stuxnet 2013-01-23  
亲,struts.xml怎么报错咧Invalid result location value/parameter
7 楼 stuxnet 2013-01-22  
求项目war包或者rar包
6 楼 stuxnet 2013-01-22  
不错求jar包,及相关的xml配置文件
5 楼 lijunaccp 2012-06-19  
xiaoyao7707 写道
楼主您好,您的http://lijunaccp.iteye.com/admin/blogs/838758

这个链接我看不了,在哪还能看到您的web.xml的配置呢


应该是可以看到的,主要内容是:

1.需要导入struts2-spring-plugin-2.1.8.1.jar包
这个包是Struts与spring整合的必须的
3.看下asm.jar文件有没有重复的,有的话保留asm.jar,删除另外一个。
4.web.xml中找到如下并改正,Struts2的核心类是下面这个。
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
5.想修改spring配置文件的位置
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/bean.xml</param-value>
</context-param>
6.web.xml例子如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <filter>
  <init-param>
  <param-name>config</param-name>
  <param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
  </init-param>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
 
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
 
 
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
 
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
</web-app>
4 楼 lijunaccp 2012-06-19  
你好,应该是可以看到的呀,主要内容如下:
3 楼 xiaoyao7707 2012-06-18  
楼主您好,您的http://lijunaccp.iteye.com/admin/blogs/838758

这个链接我看不了,在哪还能看到您的web.xml的配置呢
2 楼 lijunaccp 2011-04-02  
hxz_qlh 写道
楼主很有心啊
写的灰常仔细

不过,美中不足的是 web.xml 文件的配置漏写了哈。。。。


期待楼主将功补过!


谢谢这位兄弟提醒,我也发现了这个问题,已在这个链接中写得很详细,谢谢指正。
http://lijunaccp.iteye.com/admin/blogs/838758
1 楼 hxz_qlh 2011-04-01  
楼主很有心啊
写的灰常仔细

不过,美中不足的是 web.xml 文件的配置漏写了哈。。。。


期待楼主将功补过!

相关推荐

Global site tag (gtag.js) - Google Analytics