Structs引入bean时报空指针异常

天使小宝 天使小宝 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我现在搭建一个简单的SSH框架的实例,结果在Action中引入Bean的时候报空指针异常,希望有知道的回复下,万分感谢。



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"
id="WebApp_ID" version="2.5">
<display-name>SpringStructIBatis</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/service/spring-service.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<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>*.action</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
</servlet>

</web-app>


applicationContext.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">

<bean id="configproperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:jdbc.properties" />
</bean>

</beans>


struts.xml的内容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="comprehensive" namespace="/rpt/test" extends="base">
<action name="test" class="com.comprehensive.action.TestAction" method="toTestAction" >
<result>/jsp/test.jsp</result>
</action>
</package>

</struts>


spring-service.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<bean name="testStudent" class="com.comprehensive.model.Student" />

<bean name="testAction" class="com.comprehensive.action.TestAction" >
<property name="student">
<ref bean="testStudent" />
</property>
</bean>

</beans>


Student.java的内容:

package com.comprehensive.model;

import java.io.Serializable;

public class Student implements Serializable{

private static final long serialVersionUID = 1L;

private int num;
private String name;
private String address;
private char sex;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}

public void print(){
System.out.println("========");
}
}



TestAction.java的内容:

package com.comprehensive.action;

import com.comprehensive.model.Student;
import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private Student student;

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

public String toTestAction(){
System.out.println("=======ceshi=======");
//引入的时候会报错
student.print();
return SUCCESS;
}

}


...全文
评论 7
收藏 已收藏
分享
返回顶部
天使小宝
Java EE 67,535 成员 225,843 内容