81,092
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/WEB-INF/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="password" type="java.lang.String" />
<form-property name="user" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="com.test.struts.action.LoginAction">
<set-property property="cancellable" value="true" />
</action>
</action-mappings>
<message-resources parameter="com.test.struts.ApplicationResources" />
</struts-config>
<?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="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:test">
</property>
<property name="username" value="test"></property>
<property name="password" value="test"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/test/hibernate/entity/Customer.hbm.xml</value>
</list>
</property>
</bean>
<bean id="CustomerDAO" class="com.test.dao.impl.CustomerDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</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"
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="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="customerService" parent="txProxyTemplate" lazy-init="default" autowire="default" dependency-check="default">
<property name="target">
<bean class="com.test.service.impl.CustomerServiceImpl" lazy-init="default" autowire="default" dependency-check="default">
<property name="customerDAO">
<ref bean="CustomerDAO" />
</property>
</bean>
</property>
</bean>
</beans>
package com.test.dao;
import com.test.hibernate.entity.Customer;
public interface CustomerDAO {
public void save(Customer cus);
public void delete(Integer id);
public void update(Integer id);
public Customer getCustomerById(Integer id);
public Customer getCustomerByLoginId(String loginId);
}
package com.test.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.test.dao.CustomerDAO;
import com.test.hibernate.entity.Customer;
public class CustomerDAOImpl extends HibernateDaoSupport implements CustomerDAO {
public void delete(Integer id) {
// TODO Auto-generated method stub
this.getHibernateTemplate().delete(this.getCustomerById(id));
}
public void save(Customer cus) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(cus);
}
public void update(Integer id) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(this.getCustomerById(id));
}
public Customer getCustomerById(Integer id) {
// TODO Auto-generated method stub
return (Customer) this.getHibernateTemplate().get(Customer.class, id);
}
public Customer getCustomerByLoginId(String loginId) {
// TODO Auto-generated method stub
String hql="from Customer c where c.loginId = ?";
List <Customer> list=this.getHibernateTemplate().find(hql);
Customer cus = list.get(0);
return cus;
}
}
package com.test.service;
import com.test.hibernate.entity.Customer;
public interface CustomerService {
public boolean checkLogin(String loginId,String pass);
public void addUser(Customer user);
public Customer getCustomer(Integer id);
}
package com.test.service.impl;
import com.test.dao.CustomerDAO;
import com.test.hibernate.entity.Customer;
import com.test.service.CustomerService;
public class CustomerServiceImpl implements CustomerService {
private CustomerDAO dao;
public void addUser(Customer user) {
// TODO Auto-generated method stub
dao.save(user);
}
public boolean checkLogin(String loginId, String pass) {
// TODO Auto-generated method stub
Customer cus=dao.getCustomerByLoginId(loginId);
if(cus!=null && cus.getPassword().equals(pass))
return true;
return false;
}
public Customer getCustomer(Integer id) {
// TODO Auto-generated method stub
return dao.getCustomerById(id);
}
}
package com.test.hibernate.entity;
import java.math.BigDecimal;
/**
* Customer entity. @author MyEclipse Persistence Tools
*/
public class Customer implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 1L;
private String loginid;
private BigDecimal id;
private String password;
private String privilege;
private String status;
// Constructors
/** default constructor */
public Customer() {
}
/** full constructor */
public Customer(BigDecimal id, String password, String privilege,
String status) {
this.id = id;
this.password = password;
this.privilege = privilege;
this.status = status;
}
// Property accessors
public String getLoginid() {
return this.loginid;
}
public void setLoginid(String loginid) {
this.loginid = loginid;
}
public BigDecimal getId() {
return this.id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPrivilege() {
return this.privilege;
}
public void setPrivilege(String privilege) {
this.privilege = privilege;
}
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
}
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.test.hibernate.entity.Customer" table="CUSTOMER" schema="HP">
<id name="loginid" type="string">
<column name="LOGINID" length="10" />
<generator class="identity"></generator>
</id>
<property name="id" type="big_decimal">
<column name="ID" precision="22" scale="0" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="10" />
</property>
<property name="privilege" type="string">
<column name="PRIVILEGE" length="10" />
</property>
<property name="status" type="string">
<column name="STATUS" length="10" />
</property>
</class>
</hibernate-mapping>
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.test.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.test.service.CustomerService;
/**
* MyEclipse Struts
* Creation date: 11-05-2009
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
*/
public class LoginAction extends DispatchAction {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm loginForm = (DynaActionForm) form;// TODO Auto-generated method stub
String loginId = loginForm.getString("user");
String pass = loginForm.getString("password");
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
CustomerService svc = (CustomerService) ctx.getBean("customerService");
boolean login = svc.checkLogin(loginId, pass);
System.out.println(login==true?"true":"false");
return null;
}
}
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for DynaActionForm form</title>
</head>
<body>
<html:form action="/login">
password : <html:password property="password"/><html:errors property="password"/><br/>
user : <html:text property="user"/><html:errors property="user"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
<bean id="customerService" parent="txProxyTemplate" lazy-init="default" autowire="default" dependency-check="default">
<property name="target">
<bean class="com.test.service.impl.CustomerServiceImpl" lazy-init="default" autowire="default" dependency-check="default">
<property name="customerDAO">
<ref bean="CustomerDAO" />
</property>
</bean>
</property>
</bean>