Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 如何启用不同的';邮政';SpringMVC中的页面类型取决于用户按下的按钮?_Jsp_Spring Mvc_Http Post - Fatal编程技术网

Jsp 如何启用不同的';邮政';SpringMVC中的页面类型取决于用户按下的按钮?

Jsp 如何启用不同的';邮政';SpringMVC中的页面类型取决于用户按下的按钮?,jsp,spring-mvc,http-post,Jsp,Spring Mvc,Http Post,我的问题可能措辞不好,所以这里有一些代码 在我的控制器中,我有以下代码: @RequestMapping(value = "addGoal", method = RequestMethod.POST) public String updateGoal(@Valid @ModelAttribute("goal") Goal goal, BindingResult result) { System.out.println("result has errors: " + result.has

我的问题可能措辞不好,所以这里有一些代码

在我的控制器中,我有以下代码:

@RequestMapping(value = "addGoal", method = RequestMethod.POST)
public String updateGoal(@Valid @ModelAttribute("goal") Goal goal, BindingResult result) {

    System.out.println("result has errors: " + result.hasErrors());

    System.out.println("Goal set: " + goal.getMinutes());

    if(result.hasErrors()) {
        return "addGoal";
    } else {
        goalService.save(goal);
    }

    return "redirect:index.jsp";
}
这表示(据我所知),如果用户向
addGoal.jsp
页面发送HTTP
POST
请求:创建目标对象的实例,并将它们重定向到
index.jsp

我的问题是,如何为给定页面提供多个POST方法?例如2个按钮-1个按钮执行上述
POST
方法,页面上的不同按钮执行不同的
POST
方法

提前谢谢,如果我解释得不好,很抱歉,还在学习

This says (as far as I understand it), if a user sends an HTTP POST request to the addGoal.jsp page
不完全是这样,用户将POST发送到Spring映射的这个方法(
addGoal
),它返回(可能)WEB-INF/jsp/addGoal.jsp或/index.jsp

jsp上可以有多个表单,如果为每个表单定义不同的映射,则每个表单都会发送到不同的方法,例如:

@RequestMapping(value = "addGoal", method = RequestMethod.POST)
//addGoal code
@RequestMapping(value = "addFoul", method = RequestMethod.POST)
//addFoul code
@RequestMapping(value = "addSelfGoal", method = RequestMethod.POST)
//addSelfGoal code

然后让它们全部返回到
addGoal
,尽管此时您可能希望使用更通用的名称。

使用不同的帖子url定义一个单独的方法!我不确定我是否理解?谢谢你的回答-我需要进一步澄清的问题是如何指定调用哪个方法?它是某个HTML a:href属性吗?比如说,我有这三种方法,还有三个按钮,分别标有“添加目标”、“添加犯规”、“添加自我目标”。如何将这些按钮链接到与它们相关的特定方法?如果您使用的是html
,我们使用的另一个选项是使用表单属性来确定正确的方法。我们称之为“actionMethod”,它只是我们设置的一种隐藏输入类型。在Spring映射中,我们执行如下操作:@RequestMapping(params=“actionMethod=preivew”)