一尘不染

如何通过 和 数据从视图到控制器(Spring MVC)

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方法。

任何帮助都感激不尽。在此先感谢一吨!


阅读 237

收藏
2020-06-01

共1个答案

一尘不染

要将输入从视图传递到控制器,您需要一个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()中的工作来使用数据制作地图或列表。

2020-06-01