Maven Spring Web MVC资源不可用

时间:2016-02-26 06:09:36

标签: spring maven spring-mvc

使用Maven创建Spring MVC Web应用程序。请找到以下目录结构的图像。 web.xml,dispatcher-servlet.xml文件。我无法获得所需的index.jsp页面内容。我是新手,所以请任何人检查,让我在哪里犯错?

Web.xml中

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
  <display-name>MongoSample</display-name>
    <welcome-file-list>
            <welcome-file>/jsp/index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

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

调度-servlet.xml中

<?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:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <mvc:annotation-driven/>    
    <context:annotation-config/>
    <context:component-scan base-package="com"/>    
    <mvc:resources location="/js/" mapping="/js/**" cache-period="1314000"/>
    <mvc:resources location="/css/" mapping="/css/**" cache-period="1314000"/>

    <!-- Factory bean that creates the Mongo instance -->
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
        <property name="host" value="localhost" />
        <property name="port" value="27017"/>
    </bean>

    <!-- MongoTemplate for connecting and querying the documents in the database  --> 
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="test" />
    </bean>

    <!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean> 
</beans>

文件夹目录结构:

Folders Directory Structure

控制器:

package com.controller;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.service.UserService;
import com.model.User;
@RestController
public class UserController {

    @Autowired
    UserService userService;

    private static Logger logger = Logger.getLogger(UserController.class);

     @RequestMapping(value="/users",method = RequestMethod.GET,headers="Accept=application/json")
     public @ResponseBody List<User> getAllUsers() {     
      List<User> users=userService.getAllUserService();
      return users;
     }


     @RequestMapping(value="/users/archive/{id}",method = RequestMethod.POST,headers="Accept=application/json")
     public @ResponseBody List<User> archiveUser(@PathVariable String id) { 
         userService.archieveUserService(id);
       List<User> users=userService.getAllUserService();
      return users;
     }


     @RequestMapping(value="/users/update",method = RequestMethod.POST,headers="Accept=application/json")
         public @ResponseBody List<User> updateUser(@RequestBody User users) throws ParseException {    

            users.setLast_update_time(new Date());
         logger.info(":: Setting values in controller while updation in progress::"+users); 
           userService.updateUserService(users);

         return userService.getAllUserService();

     }

         @RequestMapping(value="/users/insert",method = RequestMethod.POST)
         public List<User> addUser(@RequestBody User users) throws ParseException { 
            Date date = new Date();
                users.setCreation_time(date);
                users.setLast_update_time(date);
            userService.saveUserService(users);
            return userService.getAllUserService();
         }                   
}

例外

java.lang.ExceptionInInitializerError
    at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:195)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:128)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:527)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:441)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:109)
    ... 16 more

Feb 26, 2016 1:14:36 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Feb 26, 2016 1:14:36 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/m] startup failed due to previous errors
Feb 26, 2016 1:14:37 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Feb 26, 2016 1:14:37 PM org.apache.catalina.core.StandardContext listenerStop
SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
    at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1071)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1045)
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:993)
    at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:583)
    at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4980)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5626)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

1 个答案:

答案 0 :(得分:0)

在Spring中你需要一个控制器(方法)来渲染jsp,这可能是一个正常的&#34;手&#34;书面控制员,或者像这样的某些人

<view-controller path="index.html*" view-name="index"/>