Last Updated
Viewed 13 Times

I want to map all my controller calls to some labels. Is there any way to do so. for example i have my controller request URL like:

app/mode/get-User-name

i want to add a label "Fetching Username" to this mapping.

First of all, here is my Controller:

@RequestMapping(value = "/esta", method = RequestMethod.POST)
public String handleRequest(HttpServletRequest request) {

    Esta estaobject = new Esta();
    // To test, if the parameters are set
    String user = request.getParameter("user");
    String name = request.getParameter("name");
    String shortname = request.getParameter("shortname");
    String was_admin_string = request.getParameter("was_admin");
    String sap_nr = request.getParameter("sap_nr");
    String etl_string = request.getParameter("etl");

    if (user != null && name != null && shortname != null && was_admin_string != null && sap_nr != null && etl_string != null) {
        some code...
    }

    request.getSession().setAttribute("esta", estaobject);

    return "esta";
}

When I visit the site, it check with the if-statement, if there are some parameters.
If not, then it should just display my form. Then, when I fill the form, it send it with POST and now there are some parameters and it goes through the if-statement.

My problem is: When I visit the site for the first time, it isn't a POST-request, so I get the error message Request method 'GET' not supported.
But change the form to a GET-request isn't a option for me. It must be POST.

So is there a solution to handle the same controller in POST and GET Requests?

Im working on an app that should have a log in screen so I did a very basic just for testing, but it doesn't works. and I debugged but I still don't get the problem, would you help me. This is the code of the controller, it has a very basic if condition but It doesn't works, I send the variables to the console, because I thought the variables were in blank but I get the variables. so I don't know what to do.

@RequestMapping(value = "ingreso.htm", method = RequestMethod.POST)
  public ModelAndView ingreso(
          @RequestParam("usuario") String usuario,
          @RequestParam("clave") String clave,
          HttpServletRequest request) {

    ModelAndView mv = new ModelAndView();
    String destino;


        logger.info(usuario);
        logger.info(clave);

      if(usuario.toString()=="295325" && clave.toString()=="7512"){
          mv.addObject("usuario", usuario);
          destino = "reservacion";
          mv.setViewName(destino);
          }
      else{
          destino = "login";
          mv.addObject("error", "usuario invalido :'v");
          mv.setViewName(destino);
          }

    return mv;
  }

Login.jsp

<form method="post" action="ingreso.htm">
        <label>Numero de cuenta</label>
        <input class="form-control" name="usuario" type="number" max-length="4" required>
        <label>Nip</label>
        <input class="form-control" name="clave" type="password"  required>
        <div class="form-group">
        <input type="submit" value="Entrar">
        </div>
        </form>

I want that the method showEnteredData is executed if the Button is pressed. But it does not execute the function. I thought it should do automaticly. What is the problem? Thanks for your help :=)

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>

    <div id="leer" style="float: left;"></div>
    <div class="filmFilter">
        <form action="articles" method="get">

            <select name="dropdownFilmeFilter">
                <option value="aufsteigend">Preis: aufsteigend</option>
                <option value="absteigend">Preis: absteigend</option>
                <option value="bewertung">Kundenbewertung</option>
                <option value="erscheinungsdatum">Erscheinungsdatum</option>
            </select>

            <input type="submit" value="Done" /> <a href="home">bring me back
                home</a>
        </form>

    </div>

@RequestMapping(value = "/articles")
public String showHome ( Model model){
    List<Article> article = (List<Article>) gd.findAll(Article.class);  
    model.addAttribute("articles", article);


    return "articles";
}


@RequestMapping(value = "/articles", method = RequestMethod.POST)
public String showEnteredData(@RequestParam("dropdownFilmeFilter") String filter, Model model) {
    System.err.println("hohooh");


    List<Article> article = (List<Article>) gd.sortByAttribute(Article.class, "price", true);
    article.clear();

    if (filter == "aufsteigend"){
        article = (List<Article>) gd.sortByAttribute(Article.class, "price", true);
    }
    model.addAttribute("articles", article);

    return "articles";
}

Similar Question 7 (3 solutions) : Zend controller's predispatch method

Similar Question 8 (3 solutions) : Spring MVC - Session - Request

Similar Question 9 (1 solutions) : Spring Controller Mapping Issues - Double Wildcard

cc