How to load the data in index page using spring mv

2019-10-03 04:17发布

I tried couple of Tutorial in Spring-MVC to load the data in index page without using ajax call means before loading the index page I want to get the data from server and load the data into the index page.but did not get proper answer.

1条回答
看我几分像从前
2楼-- · 2019-10-03 04:53

Finally after couple of try i got the answer.here is my code.

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>
查看更多
登录 后发表回答