Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SpringMVC加载索引页中的数据?_Spring_Spring Mvc - Fatal编程技术网

如何使用SpringMVC加载索引页中的数据?

如何使用SpringMVC加载索引页中的数据?,spring,spring-mvc,Spring,Spring Mvc,在Spring MVC中,我尝试了几次在索引页中加载数据的教程,但没有使用ajax调用方式。在加载索引页之前,我想从服务器获取数据并将数据加载到索引页。但没有得到正确的答案。最后,经过几次尝试,我得到了答案。这是我的代码 web.xml <display-name>SpringTest</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-

在Spring MVC中,我尝试了几次在索引页中加载数据的教程,但没有使用ajax调用方式。在加载索引页之前,我想从服务器获取数据并将数据加载到索引页。但没有得到正确的答案。

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

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>
web.xml
弹簧试验
index.jsp
春天
org.springframework.web.servlet.DispatcherServlet
1.
春天
/
上下文配置位置
/WEB-INF/spring-servlet.xml
org.springframework.web.context.ContextLoaderListener
我的控制器
@控制器
公共类网络控制器{
@自动连线
私人雇员服务;
@RequestMapping(value=“/”,method=RequestMethod.GET)
公共模型和视图索引(){
List employist=empService.getAllEmployee();
Collections.sort(employist,newemployeesortbyid());
ModelAndView modelMap=新的ModelAndView(“索引”、“员工列表”、员工列表);
System.out.println(“调用控制器”);
返回模型图;
}
}
spring-context.xml

你的问题太笼统了。试着用你正在使用的代码发布,或者更具体一点。嗨,我找到了这个问题的解决方案。。。谢谢你的更新。