2016-11-09 96 views
0

當我的web應用程序加載到tomcat上時,我遇到了一個奇怪的情況。歡迎文件得到了很好的加載,但控制器部分沒有被調用。Default RequestMapping不會被調用

這裏是我的項目目錄: enter image description here

這裏是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 

<web-app version="3.0" 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_3_0.xsd"> 

    <display-name>Archetype Created Web Application</display-name> 

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

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

    <servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value></param-value> 
     </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

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

    <welcome-file-list> 
    <welcome-file>/WEB-INF/views/index.jsp</welcome-file> 
    </welcome-file-list> 

</web-app> 

這裏是我的控制器類

package com.bng.monitor.controller; 

import java.io.PrintWriter; 
import java.text.SimpleDateFormat; 
import java.util.HashMap; 
import java.util.Map; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.json.JSONArray; 
import org.json.JSONObject; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.ui.ModelMap; 
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.servlet.ModelAndView; 

import com.bng.monitor.util.Utility; 


@Controller 
public class MonitoringController { 

    private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
    private static SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    //private static List<DBObject> pipeline=new ArrayList<DBObject>(); 
    static HashMap<String, String> hmLatestDateTime=new HashMap<String, String>(); 

    @Autowired 
    private Utility utility; 

    private static @Value("${pageRefresh}") String pageRefresh; 

    public Utility getUtility() { 
     return utility; 
    } 
    public void setUtility(Utility utility) { 
     this.utility = utility; 
    } 

    public static String getPagerefresh() { 
     return pageRefresh; 
    } 
    public void setPagerefresh(String pagerefresh) { 
     MonitoringController.pageRefresh = pagerefresh; 
    } 

    @RequestMapping(value="/version", method=RequestMethod.GET) 
    public ModelAndView getVersion(){ 
     System.out.println("inside controller.."); 
     ModelAndView mav=null; 
     try 
     { 
      mav=new ModelAndView("version"); 

      if(Utility.version!=null) 
       mav.addObject("versionapp", Utility.version); 
      else if(Utility.version == null || Utility.version.equals("")) 
       mav.addObject("versionapp", "property not defined.. :)"); 


     } catch (Exception e) { 
      e.printStackTrace(); 

     } 
     return mav; 

    } // end of getVersion() 

    @RequestMapping(value="/reloadftp",method=RequestMethod.GET) 
    public void reloadFtpConfig(){ 
     try { 
      this.utility.getFtpCred();  

     } catch (Exception e) { 
      e.printStackTrace(); 

     } 
    } 

    @RequestMapping(value="/") 
    public String displayGui(ModelMap model) { 
     try 
     { 
      System.out.println("<<< inside root mapping for index.jsp display >>>"); 

      model.addAttribute("map", Utility.hmServerFtpCred); 
      model.addAttribute("pagerefresh", pageRefresh); 
      String key=null; 
      String combinedJson=null; 

      JSONArray jsonArray=null; 
      JSONObject jsonObject1=null; 
      JSONObject jsonObject2=null; 
      String tempMod=null; 
      String tempStat=null; 
      String cpu=null; 
      String ram=null; 


      for(Map.Entry entry: Utility.hmServerFtpCred.entrySet()){ 

       key = (String) entry.getKey(); 

       // for each key get combinedJson (and do so in a loop as the json formed there may not be valid) and parse it to get module and system properties cached into hmGuiData 
       //TODO add logic to alter GUI to give some indication(color based) that the data is not updated since long time. 
       combinedJson = Utility.getStats(key); 
       jsonArray = new JSONArray(combinedJson); 

       System.out.println(">>> combined JSON: "+jsonArray.toString()); 

       JSONArray moduleArr = jsonArray.getJSONObject(0).getJSONObject("details").getJSONArray("module"); 

       for(int index=0; index<moduleArr.length() ; index++) 
       { 
        tempMod=moduleArr.getJSONObject(index).getString("module_name"); 
        tempStat=moduleArr.getJSONObject(index).getString("live"); 

        Utility.hmGuiData.put(key+"_"+tempMod.toLowerCase(), tempStat); 
       } 

       Utility.hmGuiData.put(key+"_"+"cpu", jsonArray.getJSONObject(1).getJSONObject("details").getString("cpu")); 
       Utility.hmGuiData.put(key+"_"+"ram", jsonArray.getJSONObject(1).getJSONObject("details").getString("ram")); 

       /*jsonObject1 = (JSONObject) jsonArray.get(0); 
       jsonObject2 = (JSONObject) jsonArray.get(1); 

       jsonObject1.getJSONObject("details").*/ 

      } // end of inner for 

      System.out.println(">>> Utility.hmGuiData: "+Utility.hmGuiData); 

      model.addAttribute("guidata", Utility.hmGuiData); 

      return "index"; 

     } catch (Exception e) { 
      e.printStackTrace(); 

     } 
     return null; 
    } 
     @RequestMapping(value="/getstatsone",method=RequestMethod.GET) 
    public void getStatsOne(HttpServletRequest req,HttpServletResponse resp){ 

    } 

    @RequestMapping(value="/getopco",method=RequestMethod.GET) 
    public void getOpco(HttpServletRequest req,HttpServletResponse resp) { 

      PrintWriter pw=null; 
      System.out.println(">>> inside /getopco"); 
      JSONObject json=null; 
     try 
     { 
       pw=resp.getWriter(); 
       json=new JSONObject(Utility.hmServerFtpCred); 
       pw.println(json);    
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     }finally{ 
      try { 
       if(pw!=null) 
        pw.close(); 
      } catch (Exception ee) { 

       ee.printStackTrace(); 

      } 
     } 

    } 


}//end of Controller class 

,這裏是我的應用程序的context.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:c="http://www.springframework.org/schema/c" 
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:core="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
     http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.7.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 


    <context:component-scan base-package="com.bng.monitor" /> 

    <bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location" value="/WEB-INF/config.properties" /> 
    </bean> 

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

    <mvc:resources location="/resources/css/**" mapping="/css/**"/> 
    <mvc:resources location="/resources/img/**" mapping="/img/**"/> 
    <mvc:resources location="/resources/js/**" mapping="/js/**"/> 
    <!-- <mvc:resources location="/WEB-INF/views/**" mapping="/js/**"/> --> 
    <mvc:annotation-driven /> 
    <mvc:resources mapping="/*" location="/WEB-INF/views/" /> 
</beans> 

現在發生了兩件事... 1.當我將url模式設置爲/ *併發出請求時,只有控制器被調用,歡迎jsp未加載,並且tomcat給出錯誤404,請求的資源不可用。

  1. 當我將url模式更改爲/時,只有歡迎頁面被加載,並且沒有調用後端控制器特定的映射。

在這兩種情況下,我的應用程序未加載。 請幫助我成功加載網絡應用程序。

+0

我不能給你解決,但是當你的URL模式'/ *'你的資源不會被加載,因爲他們也得到通過抓'/ *'模式。也許這可以幫助你更進一步 –

回答

0

從web.xml中刪除以下歡迎列表解決了該問題。 所以,我只是刪除從web.xml中有以下行

<welcome-file-list> 
    <welcome-file>/WEB-INF/views/index.jsp</welcome-file> 
</welcome-file-list>