action中注解问题

狄道卡卡 发布于 2015/08/07 17:15
阅读 952
收藏 0

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

@JFinal 你好,想跟你请教个问题:

是这样的,我自己创建了一个ssh框架,但是在action类中,注解service的时候就出现了问题:

问题描述:

编译运行的时候控制台没有任何错误,但是提交数据之后,表单能正常接收,此时页面就会出现在这个问题

我的具体思路是这样

首先在web.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
  <display-name></display-name>
  <welcome-file-list>
<welcome-file>Regist.jsp</welcome-file>
</welcome-file-list>
<!--spring中applicationContext加载方法:此处需要自定义加载 -->
<context-param>
<param-name>contextConfigLocation </param-name>
<param-value>/WEB-INF/classes/applicationContext-*.xml </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--strut2配置信息 -->
<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>



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:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

<!-- 使用Annocation,自动注解bean,并保证@Autowired,@Resource属性能够注入 -->
<context:annotation-config /> 
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.gsww.study"></context:component-scan>

<!--使用属性文件占位符  -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
</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>
   <constant name="struts.objectFactory" value="spring" />
   
   <package name="default" namespace="/" extends="struts-default">
      <action name="AddAction" class="com.gsww.study.action.AddAction">
          <result name="success">/index.jsp</result>
          <result name="error">/post.jsp</result>
      </action>
   </package>
</struts>    
表单:

 
  <body>
      <form action="AddAction" method="post">
          <input type="text" name="person.username" value=""/>
          <input type="text" name="person.age" value=""/>
          <input type="text" name="person.salary" value=""/>
          <input type="submit" name="submit" value="添加"/>
      </form> 

Action类:

public class AddAction extends ActionSupport{
private static final long serialVersionUID = 1L;
@Resource
private static UserOperationService userOperationService;
    private Person person;
    
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}

@Override
public String execute() throws Exception {

Person p=new Person();
p.setUsername(person.getUsername());
p.setAge(person.getAge());
p.setSalary(person.getSalary());
System.out.println(p.getUsername()+"----"+p.getAge()+"--"+p.getSalary());
if(userOperationService.add(p))
{
return SUCCESS;
}
return ERROR;
}
}
service类

import java.util.List;


import org.springframework.stereotype.Service;


import com.gsww.study.entity.Person;
import com.gsww.study.service.UserOperationService;


@Service("userOperationService")
public class UserOperationServiceImp implements UserOperationService {




@Override
public boolean add(Person person) {
System.out.println("------------------------------");
return true;
}
}


ssh框架使用的struts2.0+spring3.0, Jar包:struts2+spring-plugin2.0.1 .jar,commom-pool.jar,commom-annocation.jar,等,因为我是直接在myeclipse下直接添加的项目,所以基本包上应该不会有问题,



求大神看一下!我觉得问题应该是strut2和spring不能配合,但是我已经将action交给spring管理了,<constant name="struts.objectFactory" value="spring" />,现spring容器中有bean,但是就是action中无法注入,运行的时候只会在页面上显示空指针错误,控制台没有任何异常显示,谢谢大神。








加载中
0
Tex
Tex
你用这么臃肿的架构为什么要 @JFinal ?
0
JFinal
JFinal
   AddAction 中的 userOperationService 这个属性没有 setter 方法,所以 spring 无法为该属性进行注入,所以造成 if(userOperationService.add(p)) 这行代码产生 NullPointerException
狄道卡卡
狄道卡卡
我直接试过使用get,set注入服务,还是不行,也就是说struts2+spring3.0之间的搭配有问题。。。
0
小99
小99
看第一行 以为是JFinal的项目  ,一看是SSH 波总也许也都无奈了 哈哈
0
朱宏青
朱宏青

???不是JFinal的问题你也@波总?估计波总真的是无奈了

既然ssh不行 那就转成JFinal不就好了 

0
雨翔河
雨翔河
粗略的看了下,没有get.set,从哪儿注呢?
0
halbert918
halbert918
private static UserOperationService userOperationService;   spring不支持静态变量的注入,这个只有自己手动注入
狄道卡卡
狄道卡卡
恩恩。我再试试!
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部