struts2、spring3、hibernate3整合

本文介绍了一个使用Struts2、Spring3及Hibernate3整合的示例项目,包括必要的配置文件、核心代码及页面展示。通过具体的Action实现,展示了如何创建、查询用户,并将数据展示在前端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近公司要用struts2、spring3、hibernate3整合,自己弄了一套,网上有好多helloworld例子,我也记录下我的helloworld吧。

初了struts2,spring3,hibernate3包中的jar文件,还需要本人附件中的jar包进行引用,这些jar均来源各自官方网站下载。

 

WEB-INF/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>searchAction</welcome-file>
	</welcome-file-list>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-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>
</web-app>

src/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="com.scs.action" extends="struts-default">
		<action name="TestAction" class="com.scs.action.TestAction">
			<result name="success">/success.jsp</result>
		</action>
		<action name="regUserAction" class="com.scs.action.RegUserAction">
			<result name="success">/success.jsp</result>
			<result name="failed">/regist.jsp</result>
		</action>
		<action name="searchAction" class="com.scs.action.SearchAction">
			<result name="success">/search.jsp</result>
		</action>
		<action name="addAction" class="com.scs.action.AddAction">
			<result name="success">/add.jsp</result>
		</action>
	</package>
</struts>

src/struts.properties

struts.objectFactory = spring
struts.objectFactory.spring.autoWire = name
struts.i18n.encoding = utf-8
struts.multipart.maxSize=10000000
struts.devMode = true
struts.enable.DynamicMethodInvocation = false

src/log4j.properties

log4j.rootLogger=WARN, Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=(%r ms) [%t] %-5p: %c#%M %x: %m%n

log4j.logger.com.genuitec.eclipse.sqlexplorer=DEBUG
log4j.logger.org.apache=INFO
log4j.logger.org.hibernate=INFO

src/applicationContext.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">
	<!-- 用注解方式注入bean -->
	<context:annotation-config />
	<context:component-scan base-package="com.scs" />
	<!-- 数据库连接池-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/TestSCS" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	<!-- hibernate sessionFactory 创建 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
				<value>com/scs/pojo/TestUser.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 事物配置 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="find*" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

	<bean id="regUserAction" class="com.scs.action.RegUserAction">
  		<property name="testUserDao" ref="testUserDao"></property>
	</bean>
	<bean id="searchAction" class="com.scs.action.SearchAction">
  		<property name="testUserDao" ref="testUserDao"></property>
	</bean>
	<bean id="testUserDao" class="com.scs.dao.impl.TestUserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
</beans>

com/scs/pojo/TestUser.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.scs.pojo">
<class name="TestUser" table="TEST_USER">
	<id name="id" column="ID">
		<generator class="identity"/>
	</id>
	<property name="name" type="java.lang.String" insert="true" column="NAME"></property>
</class>
</hibernate-mapping>

com.scs.pojo.TestUser

package com.scs.pojo;

import java.io.Serializable;

public class TestUser implements Serializable {

	private static final long serialVersionUID = 1L;

	private Integer id;
	private String name;

	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

com.scs.dao.TestUserDao

package com.scs.dao;

import java.util.List;

import com.scs.pojo.TestUser;

public interface TestUserDao {
	// 创建用户
	public Integer save(TestUser user);
	// 查询用户
	public List<TestUser> search();
}

com.scs.dao.impl.TestUserDaoImpl

package com.scs.dao.impl;

import java.util.List;

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

import com.scs.dao.TestUserDao;
import com.scs.pojo.TestUser;

public class TestUserDaoImpl extends HibernateDaoSupport implements TestUserDao {

	@Override
	public Integer save(TestUser user) {
		return (Integer) getHibernateTemplate().save(user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TestUser> search() {
		return getHibernateTemplate().find("from TestUser");
	}

}

com.scs.action.AddAction

package com.scs.action;

import com.opensymphony.xwork2.ActionSupport;

public class AddAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	@Override
	public String execute() throws Exception {
		return "success";
	}
}

com.scs.action.RegUserAction

package com.scs.action;

import com.opensymphony.xwork2.ActionSupport;
import com.scs.dao.TestUserDao;
import com.scs.pojo.TestUser;

public class RegUserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private TestUserDao testUserDao;
	private TestUser testUser;

	public TestUser getTestUser() {
		return testUser;
	}

	public void setTestUser(TestUser testUser) {
		this.testUser = testUser;
	}
	
	public TestUserDao getTestUserDao() {
		return testUserDao;
	}

	public void setTestUserDao(TestUserDao testUserDao) {
		this.testUserDao = testUserDao;
	}

	public String execute() {
		testUserDao.save(testUser);
		return "success";
	}
}

com.scs.action.SearchAction

package com.scs.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.scs.dao.TestUserDao;
import com.scs.pojo.TestUser;

public class SearchAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private TestUserDao testUserDao;
	private List<TestUser> users;
	
	public TestUserDao getTestUserDao() {
		return testUserDao;
	}

	public void setTestUserDao(TestUserDao testUserDao) {
		this.testUserDao = testUserDao;
	}

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

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

	public String execute() {
		users = testUserDao.search();
		return "success";
	}
}

WebRoot/add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</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="struts2,spring3,hibernate3">
<%@ taglib prefix="s" uri="/struts-tags"%>
</head>
<body>
This is my JSP page.
<br><br>
<s:form action="regUserAction" method="get" name="testUser" >
<table width="230" border="0" align="left" height="35">
<tbody>
<tr>
<td>姓名:</td>
<td><input type="text" name="testUser.name" /></td></tr>
<tr align="center">
<td colspan="2"><input type="submit" name="submit" value="提交" /><input type="reset" value="重置" /></td>
</tr>
</tbody></table>
</s:form>
</body>
</html>

WebRoot/search.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</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="struts2,spring3,hibernate3">
</head>
<body>
<table width="230" border="1" align="left" height="35">
	<tr align="center">
		<td>ID</td>
		<td>姓名</td>
	</tr>
	<s:iterator value="users">
		<tr>
			<td><s:property value="id" /></td>
			<td><s:property value="name" /></td>
		</tr>
	</s:iterator>
</table>
<dir>
<s:a href="addAction" >增加</s:a>
</dir>
</body>
</html>

 

 

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值