如何传递<选择>和<输入>从View到Controller的数据(Spring MVC) [英] How to Pass <Select> and <input> data from View to a Controller (Spring MVC)
问题描述
您好希望一切顺利,
我正在尝试传递视图中用户填充的数据,并使用该数据执行查询。以下是HTML代码。注意:使用Thymeleaf而不是JSP
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";
}
}
我希望能够从中获取数据查看地图或列表,以便我可以将数据发送到方法fetchListByNativeSqlQuery。但是我不知道该怎么做。有人可以提供一个示例,说明如何将数据从我的View发送到控制器,从控制器发送到方法fetchListByNativeSqlQuery。
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!
推荐答案
要将视图中的输入传递给控制器,您需要一个name属性。例如,您需要以下内容:
To pass input from the view to the controller you need a name attribute. For example you need something like:
对于输入标记:
<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>
然后在控制器中,你可以使用@RequestParam,其值等于你在风景。例如:
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
从这里,你应该可以根据你的数据创建一个包含数据的地图或列表'在fetchListByNativeSqlQuery()中做。
From this, you should be able to make a map or a list with the data depending on what you're doing in fetchListByNativeSqlQuery().
这篇关于如何传递<选择>和<输入>从View到Controller的数据(Spring MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!