我正在将我的知识转移到春天,我真的不明白这是如何工作的。我正在尝试制作一个非常基本的示例以使其工作,但我无法使其工作:-((
应用程序上下文.xml
<bean id="serviceTest" class="es.mov.jose.agenda.ServiceTest">
</bean>
<bean id="serviceMovility" class="es.mov.jose.agenda.ServiceMovility">
<property name="serviceTest" ref="serviceTest" />
</bean>
<bean name="/jose/req1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="jose/req1" />
</bean>
ServiceMovility.java
package es.mov.jose.agenda;
public interface ServiceMovility {
public String callTest();
}
ServiceMovilityImpl.java
package es.mov.jose.agenda;
import es.mov.jose.agenda.ServiceMovility;
public class ServiceMovilityImpl implements ServiceMovility {
private ServiceTest serviceTest;
public String callTest() {
return serviceTest.getValue();
}
}
服务测试.java
package es.mov.jose.agenda;
public interface ServiceTest {
public String getValue();
}
ServiceTestImpl.java
package es.mov.jose.agenda;
import es.mov.jose.agenda.ServiceTest;
public class ServiceTestImpl implements ServiceTest {
public String getValue() {
return "OK";
}
}
最后是我的 req1.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="es.mov.jose.agenda.ServiceMovility"%>
<%@ page import="es.mov.jose.agenda.ServiceMovilityImpl"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="sp-forms" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<jsp:useBean id="serviceMovility" scope="request" class="es.mov.jose.agenda.ServiceMovilityImpl" />
<div class="main" >
<%
String result = serviceMovility.callTest();
out.println(result);
%>
</div>
</body>
</html>
它完美地调用 serviceMovility 但在 callTest 方法中调用 serviceTest 失败,它说 java.lang.NullPointerException ¿我应该以这种方式在 jsp 文件中包含 bean serviceMovility 吗?¿ 为什么 spring 不注入在 serviceMovility 上初始化的 serviceTest?
我花了 2 天时间,但我的所有解决方案都失败了。我不知道该怎么办。
提前致谢。