0

I am using STS 3.9.0 Tool, my project is based on Spring Boot, Thymeleaf, MySQL, Html-Bootstrap and JQuery.

In Spring Boot, during a click event the Html page needs to redirect to another page and load the dynamically generated values in Dropdown components using Jquery or Thymeleaf.

Explanation

HTML click event to call @Controller to redirecting another Html page:

@RequestMapping(value = "/getflat", method = RequestMethod.GET)
public String propertyRegistration() {
    return "FlatReg";
}

The URL will Be localhost:8080/getflat It will rediect to FlatReg.html, which is located in template folder.

and during load time, again hit @RestController URL to fetch the data from the DB and Load the data to Dropdown Controller

@PostMapping("/flatreg/saveflat")
public ResponseMsg doSaveFlatDetails() {
    ResponseMsg responseMsg = new ResponseMsg();
    try {
        // My DB call Goes Here
    } catch (Exception e) {
        // TODO: handle exception
    }
    return responseMsg;
}

ResponseMsg class Like

public class ResponseMsg {

    private String status;
    private Object dataObj;

    public ResponseMsg() {

    }

    public ResponseMsg(String status, Object dataObj) {
        super();
        this.status = status;
        this.dataObj = dataObj;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Object getDataObj() {
        return dataObj;
    }

    public void setDataObj(Object dataObj) {
        this.dataObj = dataObj;
    }
}

I don't know how to achieve this. Is there any other way? Thanks in advance.

2
  • i still not able to achive ..so please any one give some ideas... Commented Oct 30, 2017 at 9:20
  • @Kamuran Sonecek in JQuery is possible to achive using onLoad(),but i dont know how to i place the list of values into Dropdown components? Commented Oct 30, 2017 at 9:36

1 Answer 1

1

You have two ways to achieve this.

  1. Before rendering the page from the Controller method propertyRegistration(), add all the data you need to display in the page FlatReg page like so.

    @RequestMapping(value = "/getflat", method = RequestMethod.GET)
    public ModelAndView propertyRegistration() {
        ModelAndView modelAndView = new ModelAndView("FlatReg");
        modelAndView.addObject("data", service.getData()); //Get data from your service layer and set it in the ModelAndView to be later retreived in your FlatReg page.
        return "FlatReg";
    }
    
  2. The second way would be to load the "FlatReg" page as you are doing right now and then using Ajax call your Controller method doSaveFlatDetails to get the required data. Check this link on how to make an ajax call.

Hope this helps.

Sign up to request clarification or add additional context in comments.

6 Comments

how do i make page redirection in in controller page?
return "redirect:/YOUR_MAPPED_URL" where YOUR_MAPPED_URL is a url that is mapped by any of your controller method using @Requestmapping. Also please accept and upvote the answer if it has helped you in any way. Thanks
sure buddy i will try it...if succedded i will give upvote...Thanks for helping
return new ModelAndView("redirect:/FlatReg"); --- page 404 file not found error
Do you have a method annotated with @Requestmapping('/FlatReg')? /FlatReg should be mapped by any method in you Controller class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.