81,092
社区成员
发帖
与我相关
我的任务
分享
<?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>
<constant name="struts.devMode" value="true"></constant>
<package name="web" extends="struts-default">
<action name="userAction" class="com.ssh.action.UserAction" method="getALL">
<result name="getALL" type="dispatcher">/index.jsp</result>
<!-- <result name="save" type="redirectAction">addUser.action!getALL</result>
本身想重定向到action的但是上面的这行老是报错呀!!
-->
</action>
<action name="addUser" class="com.ssh.action.UserAction" method="save">
<result name="save">/index.jsp</result>
</action>
</package>
</struts>
spring配置
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.ssh.*"/>
<!-- 开启注解处理器 -->
<context:annotation-config/>
<!-- 声明切面 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<!-- 配置驱动 -->
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 注入hibernate的依赖sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入dataSource -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 扫描hibernate的映射文件 -->
<property name="mappingLocations">
<value>classpath:com/ssh/entity/*.hbm.xml</value>
</property>
<!-- hibernate的配置文件 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution (* com.ssh.service..*.*(..))" id="pointcutId"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
</aop:config>
<!-- 注入hibernate依赖hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
dao层
package com.ssh.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.ssh.entity.User;
@SuppressWarnings("unchecked")
@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public void saveUser(User u) {
this.getHibernateTemplate().save(u);
}
@Override
public List<User> getAllUser() {
return this.getHibernateTemplate().find("form User");
}
}
service层
package com.ssh.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssh.dao.UserDao;
import com.ssh.entity.User;
@Service("userService")
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public UserDao getUserDao() {
return userDao;
}
@Override
public void saveUser(User u) {
userDao.saveUser(u);
}
@Override
public List<User> getAllUser() {
return userDao.getAllUser();
}
}
action代码
package com.ssh.action;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh.entity.User;
import com.ssh.service.UserService;
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private User user;
private UserService userService;
private List<User> userList;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserService getUserService() {
return userService;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getAll(){
userList=userService.getAllUser();
return "getALL";
}
public String save() throws Exception {
userService.saveUser(user);
return "save";
}
}
其中action中的userService,service中的userDao,dao中的this.getHibernateTemplate()为空;
另外给我解释一下这几个标签作用是什么
<context:annotation-config/>
<aop:config>
<aop:pointcut expression="execution (* com.ssh.service..*.*(..))" id="pointcutId"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
<aop:aspect></aop:aspect>
</aop:config>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="com.ssh"/>
这句改成
<context:component-scan base-package="com.ssh.*"/>