2016-02-26 78 views
-1

使用Maven創建Spring MVC Web應用程序。請找到以下目錄結構的圖像。 web.xml,dispatcher-servlet.xml文件。我無法獲得所需的index.jsp頁面內容。我對此很陌生,請任何人都可以檢查並讓我知道錯誤在哪裏?Maven Spring Web MVC資源不可用

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="https://stackoverflow.com/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="https://stackoverflow.com/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="https://stackoverflow.com/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) 
+0

是JSP文件夾內的index.jsp存在? –

+0

@NallaSrinivas是的。 – Harshit

+0

在SpringMVC應用程序中,通常使用控制器(具有@Controller註釋的Java類)來處理請求並命名應執行html呈現的jsp。你有沒有這樣一個控制器,如果你沒有,那麼請張貼它(特別是返回的視圖名稱和控制器映射到的url)以及你請求的url與你的問題相關。 – Ralph

回答

0

在春天,你需要一個控制器(方法)來渲染JSP,這可能是一個正常的「手」寫控制器,或者一些服用點這樣的

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

謝謝你,爲了你的回答,我找到了問題,我解決了這個問題,但現在我得到了一些例外。將嘗試解決相同的問題。 – Harshit

+0

@哈爾吉特:請不要改變問題的範圍,而應提出一個新的問題。 – Ralph