mercredi 9 novembre 2016

Default RequestMapping does not get invoked

I am stuck with a strange situation when loading up my web application on tomcat. The welcome file gets loaded well, but the controller part is not getting invoked.

Here is my Project Directory: enter image description here

Here is my 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>

Here is my controller class

    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

and here is my 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>

Now there are two things happening... 1. When I make the url pattern as /* and make the request, then only the controller gets invoked, welcome jsp is not loaded and tomcat gives error 404, the requested resource is not available.

  1. When i change the url pattern to /, then only welcome page gets loaded and no backend controller specific mapping is called.

In both the cases, my application is not loaded. Please help me out to load the web app successfully.




Aucun commentaire:

Enregistrer un commentaire