如何将<select>和</select> <input />数据从View传递给控制器​​(Spring MVC)

时间:2015-01-28 18:47:14

标签: java html5 spring spring-mvc

您好希望一切顺利,

我正在尝试传递视图中用户填充的数据,并使用该数据执行查询。以下是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
@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。

任何帮助将不胜感激。非常感谢![/ p>

1 个答案:

答案 0 :(得分:3)

要将视图中的输入传递给控制器​​,您需要一个name属性。例如,您需要以下内容:

对于输入标记:

<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>

然后在控制器中,您可以使用@RequestParam,其值等于您在视图中指定的名称。例如:

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

由此,您应该能够根据您在fetchListByNativeSqlQuery()中所做的事情制作包含数据的地图或列表。