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!
To pass input from the view to the controller you need a name attribute. For example you need something like:
For an input tag:
For a select you need a value for each option:
Then in the controller, you can use @RequestParam with the value being equal to the name you specified in the view. For example:
From this, you should be able to make a map or a list with the data depending on what you're doing in fetchListByNativeSqlQuery().