你好,希望一切都好,
我正在尝试传递用户在 View 中填写的数据,并使用该数据执行查询。以下是 HTML 代码。注意:使用 Thymeleaf 而不是 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>
当按下提交按钮时,它会正确调用 Controller 。以下是我的 Controller 代码:
@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";
}
}
我希望能够从 map 或列表中的 View 中获取数据,以便我可以将数据发送到方法 fetchListByNativeSqlQuery。但是我不知道该怎么做。有人可以提供一个示例,说明如何将数据从 View 发送到 Controller ,以及如何从 Controller 发送到方法 fetchListByNativeSqlQuery。
任何帮助将不胜感激。提前非常感谢!
最佳答案
要将输入从 View 传递到 Controller ,您需要一个名称属性。例如,您需要类似的东西:
对于输入标签:
<input name="formation" type="text" id="formation">
对于选择,您需要为每个选项提供一个值:
<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>
然后在 Controller 中,您可以使用@RequestParam,其值等于您在 View 中指定的名称。例如:
@RequestParam(value="section") String sectionValue, @RequestParam(value="formation") String formation
由此,您应该能够根据您在 fetchListByNativeSqlQuery() 中执行的操作来制作包含数据的 map 或列表。
关于java - 如何将 <Select> 和 <input> 数据从 View 传递到 Controller (Spring MVC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28200021/