Announcement Announcement Module
Collapse
No announcement yet.
How do I access my custom bean in a webapp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I access my custom bean in a webapp

    Hi. I'm new to spring so please bear with me.

    I found this guide (http://java2t.com/233/using-spring3-...st-of-records/) on how to retrieve records from a database. It uses a normal java project and displays the results in a console. I'm trying to simulate the same example using a dynamic web project.

    my web.xml
    Code:
    <?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_3_0.xsd"
    	id="WebApp_ID"
    	version="3.0">
    	
    	<display-name>StaffDirectory</display-name>
    	
    	<servlet>
    		<servlet-name>spring</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	
    	<servlet-mapping>
    		<servlet-name>spring</servlet-name>
    		<url-pattern>*.html</url-pattern>
    	</servlet-mapping>
    
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>	
    </web-app>
    my spring-servlet.xml
    Code:
    <?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:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    		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.0.xsd">
    		
    	<context:component-scan base-package="org.flinders.staff.directory" />
    	
    	<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/test" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    	</bean>
    	
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    	
    	 <bean id="staffDirectoryDAO" class="org.flinders.staff.directory.dao.impl.StaffDirectoryDAOImpl">
    		<property name="jdbcTemplate" ref="jdbcTemplate" />
    	</bean>
    	
    	<bean id="staffDirectoryService" class="org.flinders.staff.directory.services.impl.StaffDirectoryServiceImpl">
    		<property name="staffDirectoryDAO" ref="staffDirectoryDAO" />
    	</bean>
    	
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="viewClass" >
    			<value>org.springframework.web.servlet.view.tiles2.TilesView</value>
    		</property>
    	</bean>
    	
    	<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    		<property name="definitions">
    			<list>
    				<value>/WEB-INF/tiles.xml</value>
    			</list>
    		</property>
    	</bean>	
    </beans>
    snippet of my controller class
    Code:
    package org.flinders.staff.directory.controllers;
    
    import java.util.List;
    
    import org.flinders.staff.directory.models.database.StaffModel;
    import org.flinders.staff.directory.models.misc.SearchModel;
    import org.flinders.staff.directory.services.StaffDirectoryService;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class StaffDirectoryController {
    	private StaffDirectoryService staffDirectoryService;
    	
    	public void setStaffDirectoryService(StaffDirectoryService staffDirectoryService) {
    		this.staffDirectoryService = staffDirectoryService;
    	}
    	
    	@RequestMapping("/SearchResults")
    	public void showSearchResults() {
    		//StaffDirectoryService staffDirectoryService = (StaffDirectoryService) getServletContext().getBean("StaffDirectoryService");
    	    List<StaffModel> staffList = staffDirectoryService.viewStaffResults();
    
    	    for (StaffModel staffModel : staffList) {
    
    	      System.out.println(staffModel.getStaffID() + "    : "
    
    	          + staffModel.getFirstname() + "  : " + staffModel.getSurname());
    
    	    }
    
    	    System.out.println();
    
    	}
    no problem is found during compile tim. But is found but when I try to view http://localhost:8080/mywebapp/SearchResults.html, it's throwing an exception on the line in underline (see snippet).

    Any ideas? thanks

  • #2
    I see that you are creating a staffDirectoryService bean in the context but I don't see where where you wire the bean into the controller.

    Comment


    • #3
      Originally posted by stp View Post
      I see that you are creating a staffDirectoryService bean in the context but I don't see where where you wire the bean into the controller.
      apologies for the late reply. the problem has something to do with the jsp the controller is trying to load. seems I was referencing a modelAttribute on the jsp but I forgot to pass it.

      thanks again

      Comment

      Working...
      X