SSH使用总结(xml配置)

所需要的jar包

SSH使用总结(xml配置)

beans.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop
="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

<!-- 读取jdbc.properties里的配置信息 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<bean id="dataSource" destroy-method="close"
class
="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<!-- xml方式配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/anllin/usermgr/model/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>

<!-- 开启事务管理 -->
<bean id="txManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- spring事务的xml配置 ,建议使用-->
<aop:config>
<aop:pointcut id="bussinessService"
expression
="execution(* com.anllin.usermgr.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" />
</aop:config>

<!-- 对不同的方法进行不同的事务管理 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="isExists*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>

<!-- UserDaoImpl继承 HibernateDaoSupport,即可使用hibernateTemplate-->
<bean id="userDao" class="com.anllin.usermgr.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="userService" class="com.anllin.usermgr.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>

<bean id="listUsersAction" class="com.anllin.usermgr.action.ListUserAction">
<property name="userService" ref="userService"/>
</bean>

<bean id="addUserAction" class="com.anllin.usermgr.action.AddUserAction">
<property name="userService" ref="userService"/>
</bean>

<bean id="deleteUserAction" class="com.anllin.usermgr.action.DeleteUserAction">
<property name="userService" ref="userService"></property>
</bean>

<bean id="getUserAction" class="com.anllin.usermgr.action.GetUserAction">
<property name="userService" ref="userService"></property>
</bean>

<bean id="updateUserAction" class="com.anllin.usermgr.action.UpdateUserAction">
<property name="userService" ref="userService"></property>
</bean>
</beans>

  

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="registration" namespace="/" extends="struts-default">
<action name="listUser" class="listUsersAction">
<result name="success">/listUsers.jsp</result>
</action>

<action name="addUser" class="addUserAction">
<result name="success" type="redirect">listUser.action</result>
</action>

<action name="deleteUser" class="deleteUserAction">
<result name="success" type="redirect">listUser.action</result>
</action>

<action name="getUser" class="getUserAction">
<result name="success">updateUser.jsp</result>
</action>

<action name="updateUser" class="updateUserAction">
<result name="success" type="redirect">listUser.action</result>
</action>
</package>

</struts>

  

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url
=jdbc\:mysql\://localhost\:3306/usermgr
jdbc.username
=root
jdbc.password
=123

  

log4j.properties

#
# Hibernate
, Relational Persistence for Idiomatic Java
#
# Copyright (c)
2008, Red Hat Middleware LLC or third-party contributors as
# indicated by the @author tags or express copyright attribution
# statements applied by the authors. All third-party contributions are
# distributed under license by Red Hat Middleware LLC.
#
# This copyrighted material is made available to anyone wishing to use
, modify,
# copy
, or redistribute it subject to the terms and conditions of the GNU
# Lesser General Public License
, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful
,
# but WITHOUT ANY WARRANTY
; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this distribution
; if not, write to:
# Free Software Foundation, Inc.
#
51 Franklin Street, Fifth Floor
# Boston
, MA 02110-1301 USA
#

#log4j.rootLogger
=info, stdout
log4j.appender.stdout
=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target
=System.out
log4j.appender.stdout.layout
=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern
=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger
=warn, stdout


#log4j.logger.org.hibernate
=debug
#log4j.logger.org.hibernate.test
=info
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl
=debug
#log4j.logger.org.hibernate.sql.ordering.antlr.OrderByFragmentTranslator
=trace
#log4j.logger.org.hibernate.ejb
=debug
#log4j.logger.org.hibernate.ejb.packaging
=debug
#log4j.logger.org.hibernate.reflection
=debug


#log4j.logger.org.hibernate.hql.ast.QueryTranslatorImpl
=trace
#log4j.logger.org.hibernate.hql.ast.HqlSqlWalker
=trace
#log4j.logger.org.hibernate.hql.ast.SqlGenerator
=trace
#log4j.logger.org.hibernate.hql.ast.AST
=trace
#log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder
=trace
#log4j.logger.org.hibernate.type.BasicTypeRegistry
=trace


#log4j.logger.org.hibernate.engine.Cascades
=debug
#log4j.logger.org.hibernate.hql
=debug

### log just the SQL
#log4j.logger.org.hibernate.SQL
=debug

### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type
=info
#log4j.logger.org.hibernate.type
=trace


### log HQL parse trees
#log4j.logger.org.hibernate.hql
=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache
=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc
=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider
=trace

#log4j.logger.org.jgroups
=info
#log4j.logger.org.jboss.cache
=trace
#log4j.logger.org.jboss.cache.RegionManager
=info
#log4j.logger.org.jboss.cache.lock
=info
#log4j.logger.org.jboss.cache.interceptors.PessimisticLockInterceptor
=info
#log4j.logger.org.jboss.cache.interceptors.UnlockInterceptor
=info

  

AddUserAction.java

package com.anllin.usermgr.action;

import com.anllin.usermgr.model.User;
import com.anllin.usermgr.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

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 UserService getUserService()
{
return userService;
}

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

@Override
public String execute() throws Exception
{
if(user != null)
{
this.userService.add(user);
}
return SUCCESS;
}
}

  

DeleteUserAction.java

package com.anllin.usermgr.action;

import org.apache.struts2.ServletActionContext;

import com.anllin.usermgr.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class DeleteUserAction extends ActionSupport
{
private int id;

private UserService userService;

public UserService getUserService()
{
return userService;
}

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

@Override
public String execute() throws Exception
{
id
= Integer.parseInt(ServletActionContext.getRequest().getParameter("id").trim());
if (id > 0)
{
userService.deleteById(id);
}
return SUCCESS;
}
}

  

GetUserAction.java

package com.anllin.usermgr.action;

import org.apache.struts2.ServletActionContext;

import com.anllin.usermgr.model.User;
import com.anllin.usermgr.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

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

@Override
public String execute() throws Exception
{
int id = Integer.valueOf(ServletActionContext.getRequest().getParameter("id").trim());
if(id >= 0)
{
this.user = userService.getById(id);
}

ServletActionContext.getRequest().setAttribute(
"user",user);

return SUCCESS;
}
}

  

ListUserAction.java

package com.anllin.usermgr.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.anllin.usermgr.model.User;
import com.anllin.usermgr.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class ListUserAction extends ActionSupport
{
private List<User> users;
private UserService userService;

public List<User> getUsers()
{
return users;
}

public void setUsers(List<User> users)
{
this.users = users;
}

public UserService getUserService()
{
return userService;
}

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

@Override
public String execute() throws Exception
{
users
= userService.getAll();
ServletActionContext.getContext().getSession().put(
"users",users);
return SUCCESS;
}
}

  

UpdateUserAction.java

package com.anllin.usermgr.action;

import com.anllin.usermgr.model.User;
import com.anllin.usermgr.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

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

@Override
public String execute() throws Exception
{
if(user != null)
{
userService.update(user);
}
return super.execute();
}
}

  

UserDao.java

package com.anllin.usermgr.dao;

import java.util.List;

import com.anllin.usermgr.model.User;

public interface UserDao
{
void add(User user);
void update(User user);
void delete(User user);
void deleteById(int id);
User getById(
int id);
List
<User> getByName(String username);
List
<User> getAll();
}

  

UserDaoImpl.java

package com.anllin.usermgr.dao.impl;

import java.util.List;

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

import com.anllin.usermgr.dao.UserDao;
import com.anllin.usermgr.model.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao
{
@Override
public void add(User user)
{
this.getHibernateTemplate().save(user);
}

@Override
public void delete(User user)
{
this.getHibernateTemplate().delete(user);
}

@Override
public void deleteById(int id)
{
User user
= (User)this.getHibernateTemplate().get(User.class,id);
delete(user);
}

@SuppressWarnings(
"unchecked")
@Override
public List<User> getAll()
{
return (List<User>)this.getHibernateTemplate().find("from User");
}

@Override
public User getById(int id)
{
return (User)this.getHibernateTemplate().get(User.class,id);
}

@SuppressWarnings(
"unchecked")
@Override
public List<User> getByName(String username)
{
List
<User> users = (List<User>)this.getHibernateTemplate().find("from User as u where u.username=?",username);

return users;
}

@Override
public void update(User user)
{
this.getHibernateTemplate().update(user);
}

}

  

User.java

package com.anllin.usermgr.model;

import java.sql.Date;

public class User
{
private int id;
private String username;
private String password;
private int age;
private Date birthday;

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

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 int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public Date getBirthday()
{
return birthday;
}

public void setBirthday(Date birthday)
{
this.birthday = birthday;
}

}

  

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.anllin.usermgr.model.User" dynamic-update="true" dynamic-insert="true">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="username" type="string">
<column name="username" length="20"/>
</property>
<property name="password" type="string">
<column name="password" length="20"/>
</property>
<property name="age" type="integer">
<column name="age"/>
</property>
<property name="birthday" type="date">
<column name="birthday"/>
</property>
</class>
</hibernate-mapping>

  

UserService.java

package com.anllin.usermgr.service;

import java.util.List;

import com.anllin.usermgr.model.User;

public interface UserService
{
void add(User user);
void update(User user);
void delete(User user);
void deleteById(int id);
User getById(
int id);
List
<User> getByName(String username);
List
<User> getAll();
boolean isExistsUser(String username);
}

  

UserServiceImpl.java

package com.anllin.usermgr.service.impl;

import java.util.List;

import com.anllin.usermgr.dao.UserDao;
import com.anllin.usermgr.model.User;
import com.anllin.usermgr.service.UserService;

public class UserServiceImpl implements UserService
{
private UserDao userDao;

public UserDao getUserDao()
{
return userDao;
}

public void setUserDao(UserDao userDao)
{
this.userDao = userDao;
}

@Override
public void add(User user)
{
this.userDao.add(user);
}

@Override
public void delete(User user)
{
this.userDao.delete(user);
}

@Override
public void deleteById(int id)
{
this.userDao.deleteById(id);
}

@Override
public List<User> getAll()
{
return this.userDao.getAll();
}

@Override
public User getById(int id)
{
return this.userDao.getById(id);
}

@Override
public List<User> getByName(String username)
{
return this.userDao.getByName(username);
}

@Override
public boolean isExistsUser(String username)
{
List
<User> users = getByName(username);
if(users != null && users.size() > 0)
{
return true;
}
return false;
}

@Override
public void update(User user)
{
this.userDao.update(user);
}

}

  

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>
<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>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

  

addUser.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//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Add User</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<style type="text/css">
h1
{
color
:blue;
}
</style>"
</head>

<body>
<h1>Add User</h1>
<s:form action="addUser.action" theme="simple">
username:
<s:textfield name="user.username"/><br/>
password:
<s:password name="user.password"/><br/>
age:
<s:textfield name="user.age"/><br/>
birthday:
<s:textfield name="user.birthday"/><br/>
<s:submit value="submit"/>
</s:form>
</body>
</html>

  

index.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//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
>
<html>
<head>
<base href="<%=basePath%>">

<title>User Manager</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">

<style type="text/css">
body
{
font-family
:Courial new,sans-serif;
font-size
: 16px;
}
div
{
margin
:0 auto;
text-align
: center;
background
: window;
}
div h1
{
color
:Blue;
}
div a:link
{
color
:blue;
}
div a:hover
{
color
:red;
font-size
:18px;
font-weight
: bold;
font-style
: italic;
}

</style>
</head>

<body>
<div>
<h1>User Manager</h1><br/><br/><br/>
<s:a href="listUser.action">Show All Users</s:a><br/><br/><br/>
<s:a href="addUser.jsp">Add a New User</s:a><br/>
</div>
</body>
</html>

  

listUsers.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//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>List All Users</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<style type="text/css">
table
{
border-collapse
: collapse;
}
table tr th
{
background
: MediumSlateBlue;
color
: white;
}
table tr td
{
border
:1px solid black;
background
: AliceBlue;
}
h1
{
color
:blue;
}
</style>
</head>

<body>
<h1 align="center">List All Users</h1>
<table align="center" width="80%">
<tr>
<th>username</th>
<th>password</th>
<th>age</th>
<th>birthday</th>
<th>update</th>
<th>delete</th>
</tr>
<s:iterator value="%{#session.users}">
<tr>
<td>
<s:property value="username"/>
</td>
<td>
<s:property value="password"/>
</td>
<td>
<s:property value="age"/>
</td>
<td>
<s:property value="birthday"/>
</td>
<td>
<s:a href="getUser.action?id=%{#this.id}">update</s:a>
</td>
<td>
<s:a href="deleteUser.action?id=%{#this.id}">delete</s:a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>

  

updateUser.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//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Update User</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">

<STYLE type="text/css">
h1
{
color
: blue;
}
</STYLE>
</head>

<body>
<h1>Update User</h1>
<s:form action="updateUser.action" theme="simple">
<s:hidden name="user.id" value="%{#request.user.id}"></s:hidden>
username:
<s:textfield name="user.username" value="%{#request.user.username}"/><br/>
password:
<s:textfield name="user.password" value="%{#request.user.password}"/><br/>
age:
<s:textfield name="user.age" value="%{#request.user.age}"/><br/>
birthday:
<s:textfield name="user.birthday" value="%{#request.user.birthday}"/><br/>
<s:submit value="sumbit changed"></s:submit>
</s:form>
<s:debug></s:debug>
</body>
</html>

  

你可能感兴趣的:(xml)