2

Hello hope all is well,

I am trying to pass data that is filled by a user in a view, and use that data to perform a query. The following is the HTML code. NOTE: Using Thymeleaf not JSP

<form class="form-horizontal" action="#" data-th-action="@{/waterquality/data/search-results}" data-th-object="${groundWater}" method="get" accept-charset="utf-8">

    <label id = "sectionLabel" style="text-align: right; width:109px">SECTION</label>
    <select id = "section" style = "height: 25px; width: 160px; color: black">
        <option data-th-replace="fragments/form-elements/ground-water-section-select-options :: ground-water-section-select-options">List of Sections</option>
    </select>

    <label id = "formationLabel" style="text-align: right; width:109px">Formation</label>
    <input type = "text" id = "formation" />

    <button type="submit" class="send_btn1" name="action" data-th-value="#{button.action.search}"   data-th-text="#{button.label.search}"  >Search</button>

</form>

When the submit button is pressed it correctly calls the controller. The following is my Controller code:

@Controller
@RequestMapping(value = "/waterquality/data")
public class GroundWaterSearchController {

    static Logger logger = LoggerFactory.getLogger(GroundWaterSearchController.class);

    @Autowired
    private GroundWaterService groundWaterService;

    @RequestMapping(value = {"", "/", "/search"}, method = RequestMethod.GET)
    public String showSearchForm(Model model) {

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String time = dateFormat.format(date);
        model.addAttribute("time", time);

        GroundWater groundWater = new GroundWater();
        model.addAttribute("groundWater", groundWater);

        return "waterquality/data/ground-water-search";

    }


    @RequestMapping(value = {"/search-results"}, method = RequestMethod.GET)
    public String processSearch(@Valid @ModelAttribute GroundWater groundWater,
            Model model,
            @RequestParam(value = "startPostion", defaultValue = "0") Integer startPostion,) {

        List<GroundWater> groundWaters = new ArrayList<GroundWater>();
        groundWaters = groundWaterService.fetchListByNativeSqlQuery(groundWater, viewData);

        model.addAttribute("groundWaters", groundWaters);

        return "waterquality/data/ground-water-search-results";
    }
}

I want to be able to have the data from the view in a map or list so I can send the data to a method fetchListByNativeSqlQuery. However I'm not sure how to do this. Can someone please provide an example on how I would send the data from my View to the controller, and from the controller to the method fetchListByNativeSqlQuery.

Any help will be much appreciated. Thanks a ton in advance!

1 Answer 1

3

To pass input from the view to the controller you need a name attribute. For example you need something like:

For an input tag:

<input name="formation" type="text" id="formation">

For a select you need a value for each option:

<select name="section" id="section" style="height: 25px; width: 160px; color: black">
    <option value="someValue" data-th-replace="fragments/form-elements/ground-water-section-select-options :: ground-water-section-select-options">List of Sections</option>
</select>

Then in the controller, you can use @RequestParam with the value being equal to the name you specified in the view. For example:

@RequestParam(value="section") String sectionValue, @RequestParam(value="formation") String formation

From this, you should be able to make a map or a list with the data depending on what you're doing in fetchListByNativeSqlQuery().

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

3 Comments

Makes sense for the most part, thank you for taking the time to answer. Question, when you said "For a select you need a value for each option:" what do you mean? Do I have to give each option an id or something? Or can I just give it a name and when I requestParam it will be filled with the user selection?
Yes, so just like what I did above. You should give the select a name, and then the option a value. So... value="someValue". This value will be used when you call the select's name in the controller. For example say I have <select name="animals"> <option value="cat">Cat</option> <option value="dog">Dog</option></select>. Say the user picks Cat, then when the controller gets "animals", the value "cat" will be returned.
Sounds good to me! I'll try it out and let you know. I very much appreciate your help. I have class now to 6 so I'll try around then. If it works I'll mark your answer as correct. Looks correct to me though ^_^

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.