1.在登录页面用<s:select list="#{'1':'管理员','2':'教师','3':'学生' label="选择身份" name="role"}">选择登录者的身份;
2.将用户名(username),密码(password),验证码(vercode)和身份(role)作为参数提交给继承BaseAction(自己写的Action)的LoginAction中,在BaseAction中有对AdminService(管理员业务逻辑组件)和MenuService(菜单业务逻辑组件)的注入;
3.在execute()中,如果验证码不为空,并且role为1的话(role为2、3的情况先略去),Admin admin= admintarget.findAdminByLoginInfo(username,password);,如果admin不为空的话,则开始读取相应权限的菜单,我的管理员权限是1,相对应的菜单表里面权限为1的菜单应该读取出来,但是跑到下面一句就报异常了,不知道为什么:
setMenus( menutarget.findMenuByUserPower(role));
是因为admintarget(注入的业务管理员业务逻辑组件)和menutarget(注入的菜单业务逻辑组件)不能在一起用么?单独只是登录验证就没问题,可以正常运行,到这句话就结束了,我看控制台里面把资源什么的都释放了,然后就报错,根本都不往Menu的业务逻辑层走,也就是说,menutarget.findMenuByUserPower(role)根本没调用,我的applicationContext.xml中已经对相应的Service和DAO进行了配置,自动事务代理也进行了配置,不知道是什么原因,我现在想把菜单取出来放到List里面,然后在页面里面迭代显示出来,从而实现简单的动态菜单生成,可就是取不出来啊,郁闷。
谁能帮帮我,我这号没分,送20吧,期待答复。
3 个解决方案
#1
附spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="jdbc:mysql://localhost/tssys" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="" />
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="20" />
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1" />
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="2" />
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20" />
</bean>
<!--定义了Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>Admin.hbm.xml</value>
<value>Class.hbm.xml</value>
<value>Course.hbm.xml</value>
<value>Lognote.hbm.xml</value>
<value>Menu.hbm.xml</value>
<value>Messages.hbm.xml</value>
<value>Report.hbm.xml</value>
<value>Resource.hbm.xml</value>
<value>Student.hbm.xml</value>
<value>Sysinfo.hbm.xml</value>
<value>Teacher.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<!-- 下面定义事务传播属性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
<property name="beanNames">
<!-- 下面是所有需要自动创建事务代理的bean-->
<list>
<value>menutarget</value>
<value>admintarget</value>
</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property name="interceptorNames">
<list>
<!-- 此处可增加其他新的Interceptor -->
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- ***********IAdminService的实现类定义*********-->
<bean id="admintarget"
class="com.teastusystem.service.spring.AdminServiceSpringImpl">
<property name="adminDAO" ref="adminDAO" />
</bean>
<!--**********AdminDAO object:Hibernate implementation********** -->
<bean id="adminDAO"
class="com.teastusystem.service.dao.hibernate.AdminHibernateDAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- ***********IMenuService的实现类定义*********-->
<bean id="menutarget"
class="com.teastusystem.service.spring.MenuServiceSpringImpl">
<property name="menuDAO" ref="menuDAO" />
</bean>
<!--**********MenuDAO object:Hibernate implementation********** -->
<bean id="menuDAO"
class="com.teastusystem.service.dao.hibernate.MenuHibernateDAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="jdbc:mysql://localhost/tssys" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="" />
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="20" />
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1" />
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="2" />
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20" />
</bean>
<!--定义了Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>Admin.hbm.xml</value>
<value>Class.hbm.xml</value>
<value>Course.hbm.xml</value>
<value>Lognote.hbm.xml</value>
<value>Menu.hbm.xml</value>
<value>Messages.hbm.xml</value>
<value>Report.hbm.xml</value>
<value>Resource.hbm.xml</value>
<value>Student.hbm.xml</value>
<value>Sysinfo.hbm.xml</value>
<value>Teacher.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<!-- 下面定义事务传播属性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
<property name="beanNames">
<!-- 下面是所有需要自动创建事务代理的bean-->
<list>
<value>menutarget</value>
<value>admintarget</value>
</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property name="interceptorNames">
<list>
<!-- 此处可增加其他新的Interceptor -->
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- ***********IAdminService的实现类定义*********-->
<bean id="admintarget"
class="com.teastusystem.service.spring.AdminServiceSpringImpl">
<property name="adminDAO" ref="adminDAO" />
</bean>
<!--**********AdminDAO object:Hibernate implementation********** -->
<bean id="adminDAO"
class="com.teastusystem.service.dao.hibernate.AdminHibernateDAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- ***********IMenuService的实现类定义*********-->
<bean id="menutarget"
class="com.teastusystem.service.spring.MenuServiceSpringImpl">
<property name="menuDAO" ref="menuDAO" />
</bean>
<!--**********MenuDAO object:Hibernate implementation********** -->
<bean id="menuDAO"
class="com.teastusystem.service.dao.hibernate.MenuHibernateDAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
#2
附BaseAction的代码:
package com.teastusystem.struts.action;
import com.opensymphony.xwork2.ActionSupport;
import com.teastusystem.service.IAdminService;
import com.teastusystem.service.IMenuService;
public class BaseAction extends ActionSupport {
private static final long serialVersionUID = 1L;
protected IAdminService admintarget;
protected IMenuService menutarget;
public void setMenutarget(IMenuService menutarget) {
this.menutarget = menutarget;
}
public void setAdmintarget(IAdminService admintarget) {
this.admintarget = admintarget;
}
}
package com.teastusystem.struts.action;
import com.opensymphony.xwork2.ActionSupport;
import com.teastusystem.service.IAdminService;
import com.teastusystem.service.IMenuService;
public class BaseAction extends ActionSupport {
private static final long serialVersionUID = 1L;
protected IAdminService admintarget;
protected IMenuService menutarget;
public void setMenutarget(IMenuService menutarget) {
this.menutarget = menutarget;
}
public void setAdmintarget(IAdminService admintarget) {
this.admintarget = admintarget;
}
}
#3
附LoginAction的代码:
package com.teastusystem.struts.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.teastusystem.models.Admin;
import com.teastusystem.models.Teacher;
import com.teastusystem.models.Student;
import java.util.*;
import com.teastusystem.struts.action.BaseAction;
import com.teastusystem.service.spring.MenuServiceSpringImpl;
public class LoginAction extends BaseAction {
private static final long serialVersionUID = 1L;
private Integer role;
private List menus;
private String username;
private String password;
private String vercode;
public String execute() throws Exception {
Map session = ActionContext.getContext().getSession();
System.out.println("login");
System.out.println(role);
String ver2 = (String) session.get("rand");
session.put("rand", null);
if (vercode.equals(ver2)) {
if (role == 3) {
Admin admin = admintarget.adminLogin(username, password);
if (admin != null) {
setMenus(menutarget.findMenuByMenuPower(role));
session.put("userId", admin.getAid());
session.put("userName", admin.getAusername());
session.put("userPower", admin.getApower());
return SUCCESS;
} else {
addActionError("用户名/密码不匹配");
return "error";
}
} else if (role == 2) {
return "error";
} else
return "error";
} else {
addActionError("验证码不匹配,请重新输入!");
return "error";
}
}
public String getVercode() {
return vercode;
}
public void setVercode(String vercode) {
this.vercode = vercode;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
public List getMenus() {
return menus;
}
public void setMenus(List menus) {
this.menus = menus;
}
}
package com.teastusystem.struts.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.teastusystem.models.Admin;
import com.teastusystem.models.Teacher;
import com.teastusystem.models.Student;
import java.util.*;
import com.teastusystem.struts.action.BaseAction;
import com.teastusystem.service.spring.MenuServiceSpringImpl;
public class LoginAction extends BaseAction {
private static final long serialVersionUID = 1L;
private Integer role;
private List menus;
private String username;
private String password;
private String vercode;
public String execute() throws Exception {
Map session = ActionContext.getContext().getSession();
System.out.println("login");
System.out.println(role);
String ver2 = (String) session.get("rand");
session.put("rand", null);
if (vercode.equals(ver2)) {
if (role == 3) {
Admin admin = admintarget.adminLogin(username, password);
if (admin != null) {
setMenus(menutarget.findMenuByMenuPower(role));
session.put("userId", admin.getAid());
session.put("userName", admin.getAusername());
session.put("userPower", admin.getApower());
return SUCCESS;
} else {
addActionError("用户名/密码不匹配");
return "error";
}
} else if (role == 2) {
return "error";
} else
return "error";
} else {
addActionError("验证码不匹配,请重新输入!");
return "error";
}
}
public String getVercode() {
return vercode;
}
public void setVercode(String vercode) {
this.vercode = vercode;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
public List getMenus() {
return menus;
}
public void setMenus(List menus) {
this.menus = menus;
}
}
#1
附spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="jdbc:mysql://localhost/tssys" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="" />
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="20" />
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1" />
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="2" />
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20" />
</bean>
<!--定义了Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>Admin.hbm.xml</value>
<value>Class.hbm.xml</value>
<value>Course.hbm.xml</value>
<value>Lognote.hbm.xml</value>
<value>Menu.hbm.xml</value>
<value>Messages.hbm.xml</value>
<value>Report.hbm.xml</value>
<value>Resource.hbm.xml</value>
<value>Student.hbm.xml</value>
<value>Sysinfo.hbm.xml</value>
<value>Teacher.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<!-- 下面定义事务传播属性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
<property name="beanNames">
<!-- 下面是所有需要自动创建事务代理的bean-->
<list>
<value>menutarget</value>
<value>admintarget</value>
</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property name="interceptorNames">
<list>
<!-- 此处可增加其他新的Interceptor -->
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- ***********IAdminService的实现类定义*********-->
<bean id="admintarget"
class="com.teastusystem.service.spring.AdminServiceSpringImpl">
<property name="adminDAO" ref="adminDAO" />
</bean>
<!--**********AdminDAO object:Hibernate implementation********** -->
<bean id="adminDAO"
class="com.teastusystem.service.dao.hibernate.AdminHibernateDAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- ***********IMenuService的实现类定义*********-->
<bean id="menutarget"
class="com.teastusystem.service.spring.MenuServiceSpringImpl">
<property name="menuDAO" ref="menuDAO" />
</bean>
<!--**********MenuDAO object:Hibernate implementation********** -->
<bean id="menuDAO"
class="com.teastusystem.service.dao.hibernate.MenuHibernateDAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="jdbc:mysql://localhost/tssys" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="" />
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="20" />
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1" />
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="2" />
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20" />
</bean>
<!--定义了Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>Admin.hbm.xml</value>
<value>Class.hbm.xml</value>
<value>Course.hbm.xml</value>
<value>Lognote.hbm.xml</value>
<value>Menu.hbm.xml</value>
<value>Messages.hbm.xml</value>
<value>Report.hbm.xml</value>
<value>Resource.hbm.xml</value>
<value>Student.hbm.xml</value>
<value>Sysinfo.hbm.xml</value>
<value>Teacher.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<!-- 下面定义事务传播属性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
<property name="beanNames">
<!-- 下面是所有需要自动创建事务代理的bean-->
<list>
<value>menutarget</value>
<value>admintarget</value>
</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property name="interceptorNames">
<list>
<!-- 此处可增加其他新的Interceptor -->
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- ***********IAdminService的实现类定义*********-->
<bean id="admintarget"
class="com.teastusystem.service.spring.AdminServiceSpringImpl">
<property name="adminDAO" ref="adminDAO" />
</bean>
<!--**********AdminDAO object:Hibernate implementation********** -->
<bean id="adminDAO"
class="com.teastusystem.service.dao.hibernate.AdminHibernateDAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- ***********IMenuService的实现类定义*********-->
<bean id="menutarget"
class="com.teastusystem.service.spring.MenuServiceSpringImpl">
<property name="menuDAO" ref="menuDAO" />
</bean>
<!--**********MenuDAO object:Hibernate implementation********** -->
<bean id="menuDAO"
class="com.teastusystem.service.dao.hibernate.MenuHibernateDAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
#2
附BaseAction的代码:
package com.teastusystem.struts.action;
import com.opensymphony.xwork2.ActionSupport;
import com.teastusystem.service.IAdminService;
import com.teastusystem.service.IMenuService;
public class BaseAction extends ActionSupport {
private static final long serialVersionUID = 1L;
protected IAdminService admintarget;
protected IMenuService menutarget;
public void setMenutarget(IMenuService menutarget) {
this.menutarget = menutarget;
}
public void setAdmintarget(IAdminService admintarget) {
this.admintarget = admintarget;
}
}
package com.teastusystem.struts.action;
import com.opensymphony.xwork2.ActionSupport;
import com.teastusystem.service.IAdminService;
import com.teastusystem.service.IMenuService;
public class BaseAction extends ActionSupport {
private static final long serialVersionUID = 1L;
protected IAdminService admintarget;
protected IMenuService menutarget;
public void setMenutarget(IMenuService menutarget) {
this.menutarget = menutarget;
}
public void setAdmintarget(IAdminService admintarget) {
this.admintarget = admintarget;
}
}
#3
附LoginAction的代码:
package com.teastusystem.struts.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.teastusystem.models.Admin;
import com.teastusystem.models.Teacher;
import com.teastusystem.models.Student;
import java.util.*;
import com.teastusystem.struts.action.BaseAction;
import com.teastusystem.service.spring.MenuServiceSpringImpl;
public class LoginAction extends BaseAction {
private static final long serialVersionUID = 1L;
private Integer role;
private List menus;
private String username;
private String password;
private String vercode;
public String execute() throws Exception {
Map session = ActionContext.getContext().getSession();
System.out.println("login");
System.out.println(role);
String ver2 = (String) session.get("rand");
session.put("rand", null);
if (vercode.equals(ver2)) {
if (role == 3) {
Admin admin = admintarget.adminLogin(username, password);
if (admin != null) {
setMenus(menutarget.findMenuByMenuPower(role));
session.put("userId", admin.getAid());
session.put("userName", admin.getAusername());
session.put("userPower", admin.getApower());
return SUCCESS;
} else {
addActionError("用户名/密码不匹配");
return "error";
}
} else if (role == 2) {
return "error";
} else
return "error";
} else {
addActionError("验证码不匹配,请重新输入!");
return "error";
}
}
public String getVercode() {
return vercode;
}
public void setVercode(String vercode) {
this.vercode = vercode;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
public List getMenus() {
return menus;
}
public void setMenus(List menus) {
this.menus = menus;
}
}
package com.teastusystem.struts.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.teastusystem.models.Admin;
import com.teastusystem.models.Teacher;
import com.teastusystem.models.Student;
import java.util.*;
import com.teastusystem.struts.action.BaseAction;
import com.teastusystem.service.spring.MenuServiceSpringImpl;
public class LoginAction extends BaseAction {
private static final long serialVersionUID = 1L;
private Integer role;
private List menus;
private String username;
private String password;
private String vercode;
public String execute() throws Exception {
Map session = ActionContext.getContext().getSession();
System.out.println("login");
System.out.println(role);
String ver2 = (String) session.get("rand");
session.put("rand", null);
if (vercode.equals(ver2)) {
if (role == 3) {
Admin admin = admintarget.adminLogin(username, password);
if (admin != null) {
setMenus(menutarget.findMenuByMenuPower(role));
session.put("userId", admin.getAid());
session.put("userName", admin.getAusername());
session.put("userPower", admin.getApower());
return SUCCESS;
} else {
addActionError("用户名/密码不匹配");
return "error";
}
} else if (role == 2) {
return "error";
} else
return "error";
} else {
addActionError("验证码不匹配,请重新输入!");
return "error";
}
}
public String getVercode() {
return vercode;
}
public void setVercode(String vercode) {
this.vercode = vercode;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
public List getMenus() {
return menus;
}
public void setMenus(List menus) {
this.menus = menus;
}
}