【问题标题】:Maven Spring Web MVC resource not availableMaven Spring Web MVC 资源不可用
【发布时间】:2016-02-26 06:09:35
【问题描述】:

使用 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>

dispatcher-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>

文件夹目录结构:

控制器:

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)

【问题讨论】:

  • jsp文件夹里面有没有index.jsp?
  • 在 SpringMVC 应用程序中,通常使用控制器(带有 @Controller 注释的 java 类)来处理请求并命名应该进行 html 呈现的 jsp。你有这样的控制器吗?如果你有,请发布它(特别是返回的视图名称,以及控制器映射到的 url)以及你请求的 url 与你的问题相关。
  • @Ralph,它是一个休息控制器。初始加载页面不可用。
  • 您是否收到 404 错误?
  • 你能粘贴那个异常吗

标签: spring maven spring-mvc


【解决方案1】:

在 Spring 中,您需要一个控制器(方法)来渲染 jsp,这可能是一个普通的“手写”控制器,或者类似的东西

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

【讨论】:

  • 感谢您的回答,我发现了问题并解决了这个问题,但现在我遇到了一些例外情况。将尝试解决相同的问题。
  • @Harshit:请不要改变问题的范围,而是提出一个新的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多