Announcement Announcement Module
Collapse
No announcement yet.
Form action="*.htm?action=" does not work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Form action="*.htm?action=" does not work

    this is jsp fle:
    <form id="frmmain" name="frmmain" action="barChart.htm?action=getFile" method="POST">

    <select name="sel">
    <option value="Alto">Alto</option>
    <option value="Esteem">Esteem</option>
    <option value="Honda City">Honda City</option>
    <option value="Chevrolet">Chevrolet</option>
    <input type="Submit" value="Display Chart">
    </form>

    <%
    String st = request.getParameter("sel");
    if (st != null) {
    out.println("You have selected: " + st);
    }
    %>

    Here, '?action=getFile' is a method of controller, when run this apps, 'getFile' is not called, but execute
    statement 'out.println("You have selected: " + st);'.
    I use spring controller.

    Please help

  • #2
    Show contoller code.

    Comment


    • #3
      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      package springapp.web;

      /**
      *
      * @author */
      import java.io.BufferedReader;
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import org.springframework.web.servlet.ModelAndView;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import java.io.IOException;
      import java.net.BindException;
      import java.util.Date;
      import java.util.LinkedHashMap;
      import java.util.Map;
      import org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController;
      import org.springframework.web.util.WebUtils;
      import springapp.service.ProcessFileManager;

      public class ChartController extends MultiActionController {

      protected final Log logger = LogFactory.getLog(getClass());

      private ProcessFileManager processFileManager;

      /**
      *
      * @param request
      * @param response
      * @return
      * @throws ServletException
      * @throws IOException
      * @throws Exception
      */
      @Override
      public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, Exception {

      String now = (new Date()).toString();

      logger.info("Returning hello view with " + now);

      // return new ModelAndView("WEB-INF/jsp/hello.jsp", "now", now);
      // see <bean id="viewResolver" in springapp-servlet.xml
      // return new ModelAndView("hello", "now", now);

      // read text file - user space usage
      Map<String, String> hm = new LinkedHashMap<String, String>();

      try {
      BufferedReader in = new BufferedReader(new FileReader("D:/zzzz/spaceMonitor/myfile.txt"));

      String line;
      String[] values;

      System.out.println("============================== ========ChartController: Display map data:");

      while (((line = in.readLine()) != null)) {

      values = line.split(":");

      // Check the line to ensure that both user and storage are present
      // If not, will not add them into the map
      if (values.length > 1) {
      String user = values[0];
      String size = values[1];

      hm.put(user, size);

      //ok for list map data line by line
      //System.out.println("user: " + user + " " + size);
      }

      }
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }

      System.out.println(" ( Display all map data line by line )");
      System.out.println("============================== ============== End");
      System.out.println(" ");
      /*
      HashMap<String, String> hm = new HashMap<String, String>();

      hm.put("user1", "123 GB");
      hm.put("user2", "120 GB");
      hm.put("user3", "105 GB");
      hm.put("user4", "100 GB");
      hm.put("user5", "98 GB");
      hm.put("user6", "70 GB");
      */

      // return new ModelAndView("WEB-INF/jsp/hello.jsp", "now", now);
      // see <bean id="viewResolver" in springapp-servlet.xml
      // return new ModelAndView("hello", "now", now);

      //drop down list is displayed first, top page 'readFolder.jsp' for display
      //================================================== ===================== Drop down list
      ModelAndView modelAndView = new ModelAndView("barChart3");
      //ModelAndView modelAndView = new ModelAndView("readFolder");
      //ModelAndView modelAndView = new ModelAndView("testPage1");

      //ok for display bar chart, top page 'barChart.jsp' for display
      //================================================== ===================== barChart.jsp
      // ModelAndView modelAndView = new ModelAndView("barChart3");


      modelAndView.addObject("now", now);

      modelAndView.addObject("userHashMap", hm);

      System.out.println("----------------------------------------------------ChartController.java:");
      //System.out.println("111 aaa ::: Storage for user1: " + hm.get("user2"));

      //ok for display hm and modelAndView
      System.out.println(" hm: " + hm);
      //System.out.println(" modelAndView: " + modelAndView);

      System.out.println(" ( modelAndView: barChart.jsp )");
      System.out.println("----------------------------------------------------End");
      System.out.println(" ");

      //just testing
      boolean b = WebUtils.hasSubmitParameter(request, "getFile");
      logger.info("testing only = " + b);

      return modelAndView;

      }


      public ModelAndView getFile(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, Exception {

      String fileName = request.getParameter("sel");


      //logger.info("=================HelloController : fileName:" + fileName);

      Map<String, String> map = processFileManager.getFileData(fileName);

      System.out.println("mapsize: " + map.size());

      System.out.println("ChartController: getFile");
      System.out.println("============================== =============ChartController.java : getFile");
      System.out.println("file name: " + fileName);

      ModelAndView modelAndView = new ModelAndView("barChart2");
      modelAndView.addObject("fileName", fileName);
      modelAndView.addObject("map", map);

      return modelAndView;
      }

      // bean injection
      public void setProcessFileManager (ProcessFileManager processFileManager) {
      this.processFileManager = processFileManager;
      }

      //11/7 for testing
      protected ModelAndView onSubmit(HttpServletRequest request,
      HttpServletResponse response, Object command, BindException errors)
      throws Exception {

      //log.debug("onSubmit: " + command);

      if (WebUtils.hasSubmitParameter(request, "getFile")) {
      return getFile(request, response);
      }

      return getFile(request, response);
      }

      }

      this is my controller, in jsp page, cannot call method 'getFile'.

      thanks
      Last edited by gis2013; Jul 14th, 2013, 08:18 PM.

      Comment


      • #4
        First - If 'barChart' is alias on 'ChartController' then try
        Code:
        <form id="frmmain" name="frmmain" action="barChart.htm?getFile" method="POST">
        Second - logic in controller is a bad practice.
        Third - I would advise to use annotations.
        P.s. in code
        Code:
        if (WebUtils.hasSubmitParameter(request, "getFile")) {
        return getFile(request, response);
        }
        You search parameter 'getFile', but this is not a parameter. Parameter is 'action' with value 'getFile'.
        If I don't mistaken.
        Last edited by Fire; Jul 15th, 2013, 02:21 AM.

        Comment


        • #5
          Thanks !

          'barChart' is a jsp. 'getFile' is a method in 'ChartController'.

          'action' of '?action=getFile' is defined in springapp-servlet.xml:
          <bean id="paramResolver" class="springapp.web.DefaultParameterMethodNameRes olver">
          <property name="paramName" value="action" />
          <property name="defaultMethod" value="handleRequest" />
          </bean>

          Comment

          Working...
          X