如何使用spring mvc在索引页面中加载数据?

时间:2015-04-01 12:40:21

标签: spring spring-mvc

我在Spring-MVC中尝试了几个Tutorial来加载索引页面中的数据而不使用ajax调用意味着在加载索引页面之前我想从服务器获取数据并将数据加载到索引页面中。但是没有得到正确答案。

1 个答案:

答案 0 :(得分:0)

经过几次尝试,我得到了答案。这是我的代码。

web.xml




 <display-name>SpringTest</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

 <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>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


My Controller 



 @Controller
    public class WebController {

        @Autowired
        private EmployeeService empService;


       @RequestMapping(value = "/", method = RequestMethod.GET)
       public ModelAndView index() {
           List<Employee> empList = empService.getAllEmployee();
           Collections.sort(empList, new EmployeeSortById());
           ModelAndView modelMap = new ModelAndView("index","employeeList", empList);
           System.out.println("Calling controller");
           return modelMap;
       }
    }



spring-context.xml


<context:component-scan base-package="com.app.controller,com.app.dao.impl,com.app.service.impl" />
   <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>