不会调用默认的RequestMapping

时间:2016-11-09 13:23:31

标签: java spring-mvc tomcat web web-deployment

在tomcat上加载我的Web应用程序时,我遇到了一个奇怪的情况。欢迎文件加载良好,但控制器部分未被调用。

这是我的项目目录: 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

这是我的application-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模式更改为/时,只会加载欢迎页面,并且不会调用后端控制器特定的映射。
  2. 在这两种情况下,我的应用程序都没有加载。 请帮我成功加载网络应用程序。

1 个答案:

答案 0 :(得分:0)

从web.xml中删除以下欢迎列表解决了这个问题。 所以,我刚从web.xml中删除了以下几行

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