81,122
社区成员




<?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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<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>
</web-app>
<?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">
<import resource="com/common/commonContext.xml"/>
<bean id="optLoggerBean"
class="com.common.OptLoggerBean">
</bean>
<bean id="exceptionLogger"
class="com.common.ExceptionLogger">
</bean>
<aop:config>
<aop:pointcut id="actionPoint"
expression="within(com.*.action..*)"/>
<aop:aspect id="optLoggerAspect"
ref="optLoggerBean">
<aop:around pointcut-ref="actionPoint"
method="logger"/>
</aop:aspect>
<aop:aspect id="exceptionAspect"
ref="exceptionLogger">
<aop:after-throwing
pointcut-ref="actionPoint"
method="logger"
throwing="e"/>
</aop:aspect>
</aop:config>
<bean id="stuDao" class="com.huangcf.dao.impl.StudentDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="ser" class="com.huangcf.service.impl.StudentServiceImpl">
<property name="dao" ref="stuDao"></property>
<property name="transactionTemplate" ref="transactionTemplate" />
</bean>
<bean id="studentAction" class="com.huangcf.action.StudentAction"
scope="prototype">
<property name="ser" ref="ser"></property>
<property name="username">
<value type="java.lang.String">admin</value>
</property>
<property name="password">
<value type="java.lang.String">admin</value>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:ORCL</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>Aa123456</value>
</property>
<property name="maxActive">
<value>255</value>
</property>
<property name="maxIdle">
<value>2</value>
</property>
<property name="maxWait">
<value>120000</value>
</property>
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
<!--
<bean id="jdbctTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" >
</property>
</bean>
<bean
id="JdbcBasicTxProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<description>数据库事务代理</description>
<property name="transactionManager" ref="jdbctTxManager"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean
id="BasicJdbcDAO" abstract="true"
class="org.springframework.jdbc.core.support.JdbcDaoSupport">
<description>JdbcDAO</description>
<property name="dataSource" ref="dataSource"/>
</bean> -->
</beans>
package com.huangcf.action;
import com.common.BaseAction;
import com.huangcf.entity.Student;
import com.huangcf.service.StudentService;
public class StudentAction extends BaseAction {
public String username;
public String password;
private StudentService ser;
@Override
public String execute(){
try{
Student student = new Student();
student.setUsername(username);
student.setPassword(password);
ser.login(student);
}catch(Exception e){
e.printStackTrace();
logger.error(e);
}
return SUCCESS;
}
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 StudentService getSer() {
return ser;
}
public void setSer(StudentService ser) {
this.ser = ser;
}
}
package com.huangcf.service.impl;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import com.huangcf.dao.StudentDao;
import com.huangcf.entity.Student;
import com.huangcf.service.StudentService;
public class StudentServiceImpl implements StudentService {
private StudentDao dao;
private TransactionTemplate transactionTemplate;
public boolean login(final Student stu) {
transactionTemplate.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus arg0) {
boolean b = dao.login(stu);
System.out.println(b);
return b;
//throw new RuntimeException("抛出异常,在transactionTemplate中回调的事务将会回滚");
}
});
return false;
}
public StudentDao getDao() {
return dao;
}
public void setDao(StudentDao dao) {
this.dao = dao;
}
public TransactionTemplate getTransactionTemplate() {
return transactionTemplate;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
}
package com.huangcf.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.common.BaseDAO;
import com.huangcf.dao.StudentDao;
import com.huangcf.entity.Student;
public class StudentDaoImpl extends BaseDAO implements StudentDao {
public boolean login(Student stu) {
String sql = "select login_name username,password password from tb_user where login_name = 'admin' and password = 'admin'";
Student item = queryForObject(sql, Student.class);
if(item == null){
return false;
}
return true;
}
}