HTTP Status 500 -(struts2整合进spring2.5和hibernate3.3)

来源:互联网 发布:剑网3天策超帅捏脸数据 编辑:程序博客网 时间:2024/05/18 03:44

myeclipse 的控制台没报错,但是浏览器报错:

 

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

Unable to instantiate Action, loginAction,  defined for 'login' in namespace '/employee'Error creating bean with name 'loginAction' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'employeeService' of bean class [com.hsp.web.action.loginAction]: Bean property 'employeeService' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:307)com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

root cause

org.springframework.beans.factory.BeanCreationException........
...
...
 
 
 
 

 

这里的bug并不是由于struts2整合引起的,而是自己不小心,在spring的配置中粗心大意,在下以为此类bug易被忽视,调试又不能准确找到源头

 

先介绍是哪里错了,在介绍struts2整合进spring2.5和hibernate3.3的一种简易方法:

1.在ss2h中,很多人在model层建一个interfaces包和一个impl包,这里以com.web.interfaces和com.web.impl为例,两个包分别各有一个类:EmployeeInter.java和EmployeeImpl.java(这里的例子简单粗暴,是为了方便讲解)

EmployeeInter.java类:

...

public interface EmployeeInter {
 
 //声明一些方法(增,删,改,查)
 public void addEmployee(Employee e);
 
 public void delEmployee(java.io.Serializable id);
 
 public void updEmployee(Employee e);
 
 public List<Employee> showEmployee();

 //验证用户
 public Employee checkuser(Employee e);
 

}

EmployeeImpl.java类:

...

@Transactional
public class EmployeeImpl implements EmployeeInter {
 
 private SessionFactory sessionFactory;
   
 //添加一个雇员
 public void addEmployee(Employee e) {
  // TODO Auto-generated method stub
  
  Session s=sessionFactory.getCurrentSession();  
  s.save(e);

 }

 public void delEmployee(Serializable id) {
  // TODO Auto-generated method stub

 }

 public List<Employee> showEmployee() {
  // TODO Auto-generated method stub
  return null;
 }

 public void updEmployee(Employee e) {
  // TODO Auto-generated method stub

 }

public Employee checkuser(Employee e) {
  // TODO Auto-generated method stub
  String hql="from Employee where id=? and pwd=?";
  List<Employee> list=sessionFactory.getCurrentSession().createQuery(hql).setString(0,e.getId()+"").setString(1,e.getPwd()).list();
  if(list.size()==1)
  {
   return list.get(0);
  }
  else{
   return null;
  }
 }

 public void setSessionFactory(SessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }

...

一个Action类:

...

private EmployeeInter employeeInter;

public String execute(){
  Employee employee=new Employee();
  employee.setId(Integer.parseInt(id));
  employee.setPwd(pwd);
  employee=(Employee)employeeInter.checkuser(employee);
  if(employee!=null)
  {
   return "loginsuccess";
  }
  else{
   return "loginfail";
  }

...

注意看到,Action类中用了employeeInter

下面再看spring的applicationContext.xml文件:

  <bean id="employeeImpl" class="com.web.impl.EmployeeImpl">
  <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

<bean id="loginAction" class="com.web.action.loginAction" scope="prototype">
  <property name="employeeImpl" ref="employeeImpl"/>
  </bean>

错误根源:

  <property name="employeeImpl" ref="employeeImpl"/>

很多人写代码写的快就会出现此类小差错

正确写法:

  <property name="employeeInter" ref="employeeImpl"/>

只是一个小小的不起眼的失误可能会让你调个够。

 

下面介绍一种struts2整合进spring2.5和hibernate3.3的方法,前提是spring2.5与hibernate3.3已经配好,web容器能初始化spring且配置了struts2

1.检查有没有引进struts2-spring-plugin-2.1.8.jar包,一定要引进!

2.结合上面代码,下面是struts.xml文件的一部分:

<constant name="struts.objectFactory" value="spring"/>

<package name="employee" namespace="/employee" extends="struts-default">
<action name="login" class="loginAction" method="execute">
<result name="loginsuccess">/WEB-INF/mainFrame.jsp</result>
<result name="loginfail">/WEB-INF/error.jsp</result>
</action>
</package>
</struts>

 

<constant name="struts.objectFactory" value="spring"/>是指Action给spring容器接管

<action name="login" class="loginAction" method="execute">是指Action的名称与spring的bean相关联,class=“”与前文spring的applicationContext.xml的其中一个bean的id一致!一定要一致!

只需配置这两项即可

 

0 0