Reputation: 13698
My applicationContext.xml:
<bean id="studentService" class="com.coe.StudentService">
<property name="studentProfile" ref="studentProfile" />
</bean>
<bean id="studentProfile" class="com.coe.student.StudentProfile">
</bean>
My web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
My classes:
StudentService{
private StudentProfile studentProfile;
//has appropriate getters/setters
}
StudentProfile{
private String name;
//has getter/setter
}
i have a jsp that calls studentService.studentProfile.name, and the error says that studentProfile is null
My assumption is that when the server starts up, Spring instantiates all objects as requested, so when the StudentService is called, would not Spring also set StudentProfile?
Upvotes: 0
Views: 1070
Reputation: 50237
Normally with Spring and a web-app, you would have a DispatcherServlet and a bunch of controllers, that pass onto JSP views. These controllers would be managed by Spring.
If you want to go direct to a JSP without using DispatcherServlet, then you need some way to make the first injection into your pages (ContextLoaderListener doesn't do this). That is, you must explicitly lookup the initial bean using JSP initialization code such as
[Disclaimer: not tested]
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%!
private StudentService studentService;
public void jspInit() {
studentService = (StudentService) WebApplicationContextUtils.
getRequiredWebApplicationContext(getServletContext()).
getBean("studentService");
}
%>
Upvotes: 2
Reputation: 20732
Not really an answer to your question, but a possible solution to your problem, if you're willing to work with annotations instead:
Web.xml
<!-- Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>spring.xml</param-value>
</context-param>
Spring.xml
<context:component-scan base-package="com.coe" />
Java code
@Service
StudentService{
@Autowired
private StudentProfile studentProfile; }
@Repository//???
StudentProfile{
private String name;}
That said I have a little trouble understanding why StudentProfile would be a bean (assuming every student has a profile) and StudentService would have reference to a single StudentProfile, but that might just be your terminology.. (Or my lack of understanding thereof)
Upvotes: 0
Reputation:
perhaps your name property is the null item. try setting a value
<property name="name" value="my value"/>
Upvotes: 1