bo中存放实体类
Action中
private Users users;
private String userName;
private String password;
private String msg;//需要get set
public String login(){
if(userName.equals("")||password.equals("")){
msg="用户名和密码不能为空";
return "login";
}else{
Users u=usersService.login(userName, password);
//存session 前台调用
//用struts标签: <s:property value="#session.user.adminName" />
//用EL表达式:${session.user.scadminName}
//用java代码:<%=session.getAttribute("user.adminName")%>
if(u!=null){
HttpServletRequest request=(HttpServletRequest) ServletActionContext.getRequest();
HttpSession session=request.getSession();
session.setAttribute("user", u);
return "ok";
}else{
msg="用户名或密码不正确";
return "login";
}
}
}
UserDao中
public Users login(String userName,String password);
UsersDAOImpl中
public Users login(String userName,String password) {
List<Users> list=this.getHibernateTemplate().find("from Users where userName=? and password=?",userName,password);
Users u=new Users();
if(list.size()>0){
u.setUserid(list.get(0).getUserid());
u.setUserName(list.get(0).getUserName());
u.setPassword(list.get(0).getPassword());
u.setEmil(list.get(0).getEmil());
}else{
return null;
}
return u;
}
UsersService中
public Users login(String userName,String password);
UsersServiceImpl
private UsersDAO usersDAO;//get set
public Users login(String userName,String password){
return usersDAO.login(userName, password);
}
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=Text"></property>
<property name="username" value="sa"></property>
<property name="password" value="qaz123"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/ssh/bo/Users.hbm.xml</value>
<value>com/ssh/bo/Menu.hbm.xml</value></list>
</property></bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="baseTxProxy" abstract="true" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_NOT_SUPPORTED</prop>
<prop key="count*">PROPAGATION_NOT_SUPPORTED</prop>
<prop key="select*">PROPAGATION_NOT_SUPPORTED</prop>
<prop key="find*">PROPAGATION_NOT_SUPPORTED</prop>
<prop key="load*">PROPAGATION_NOT_SUPPORTED</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="import*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="batch*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="del*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="save">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="union*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- action -->
<bean id="usersAction" class="com.ssh.action.UsersAction" scope="prototype">
<property name="usersService" ref="usersService"></property>
</bean>
<!-- service -->
<bean id="usersService" class="com.ssh.service.impl.UsersServiceImpl">
<property name="usersDAO" ref="usersDAO"></property>
</bean>
<!-- dao -->
<bean id="usersDAO" class="com.ssh.dao.impl.UsersDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
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="struts_pub" namespace="/" extends="struts-default">
<action name="login" class="com.ssh.action.UsersAction" method="login">
<result name="login">/index.jsp</result>
<result name="ok">/Home.jsp</result>
</action>
</package>
</struts>
index.jsp
上面增加标签库 <%@ taglib uri="/struts-tags" prefix="s"%>
返回的信息<a class="msg"><s:property value="msg"/></a>
<form action="login" method="post">
<a class="msg"><s:property value="msg"/></a>
用户名:<input type="text" id="userName" name="userName" />
密码:<input type="password" id="password" name="password"/>
<input type="submit" value="登陆" />
</form>