1

can anyone help me. I cant understand, why @RequestParameter or request.getParameter() not working.

My controller:

@Controller
public class CheatController extends WebMvcConfigurerAdapter {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(@RequestParam("gg") String gg, Model model) {
        return "hello";
    }
}

And my view:

<html>
<body>
<form action="#" th:action="@{/hello}" method="get">
    <input type="text" id="gg" name="gg" placeholder="Your data"/>
    <input type="submit"/>
</form>
<span th:if="${gg != null}" th:text="${gg}">Static summary</span>
</body>
</html>
Mahozad
  • 18,032
  • 13
  • 118
  • 133
Roman
  • 21
  • 4

3 Answers3

0

Seems like you have an error in the @RequestParam

Try replacing this line public String hello(@RequestParam("gg") String gg, Model model) by:

public String hello(@RequestParam(required = false, defaultValue = "") String gg, Model model)

What we're setting in the line above is that gg is not required and if your param gg comes empty or null the defaultValue will be "". You can remove this options but is a good way to test that the Controller is working, and if you know for sure that you're going to receive always a gg param you can remove it.

juanlumn
  • 6,155
  • 2
  • 30
  • 39
0

You should be using POST instead of GET on your form:

<form action="#" th:action="@{/hello}" method="get">

You can also simplify your controller code to:

@Controller
public class CheatController {

   @GetMapping("/hello")
   public String hello(@RequestParam("gg") String gg, 
                       Model model) {
      ...
      return "hello";
   }
}
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
0

I cant understand how it influenced on getting and sending params, but it helped me(i commented that peace of code and it started working). Can anyone explain why it happened?

@Configuration
public class DefaultView extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers( ViewControllerRegistry registry ) {
        //registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/all").setViewName("all");

        registry.setOrder( Ordered.HIGHEST_PRECEDENCE );
        super.addViewControllers( registry );
    }
} 
Roman
  • 21
  • 4