Html 非常简单的spring MVC按钮点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20193138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:14:25  来源:igfitidea点击:

Very simple spring MVC button click

htmlspring-mvc

提问by user101010101

I have the following code in my SampleController;

我的 SampleController 中有以下代码;

@Controller
public class SampleController {

    @RequestMapping("home")
    public String loadHomePage(Model m) {
        m.addAttribute("name", "CodeTutr");
        return "home";
    }

    @RequestMapping(value="/test", method=RequestMethod.GET)
    public String handlePost(@RequestParam String action, Model m) {
        if( action.equals("save") ){
            //handle save
         }
         else if( action.equals("renew") ){
            //handle renew
         }
        m.addAttribute("name", "change");
        return "home";
    }
}

on page load the attribute I set it successful shown on the web page. I am trying to get my head around button clicks on spring mvc below is my jsp code;

在页面加载属性我设置它成功显示在网页上。我试图让我的头脑围绕 spring mvc 上的按钮点击,下面是我的 jsp 代码;

<!DOCTYPE HTML>
<html>
  <head>
    <title>Sample Application</title>
  </head>
  <body>
    <h1>Hello, ${name}!</h1>
    <input type="submit" name="action" value="save" />
  </body>
</html>

My input does not do anything, the method handlePost is never hit. I was trying to change the attribute "name" to the word "change", I am not sure what I am doing incorrectly.

我的输入没有做任何事情,方法 handlePost 永远不会被命中。我试图将属性“名称”更改为“更改”一词,但我不确定我做错了什么。

回答by Sotirios Delimanolis

Your issue isn't with Spring, it's with HTML. You cannot submit a button. You can only submit a <form>.

您的问题不在于 Spring,而在于 HTML。您不能提交按钮。您只能提交一个<form>.

Wrap your <input>element in a <form>

将您的<input>元素包裹在一个<form>

<form action="<c:url value="/test" />" method="GET">
    <input type="submit" name="action" value="save" />
</form>

Where <c:url>is the urltag of the coretaglib. Now, when you click the button, the browser will serialize your <input>elements as url-encoded form parameters and send them. They will appear as request parameters to your server/web application. Spring will deserialize them by name and inject them as arguments where you have a @RequestParammethod parameter.

taglib<c:url>url标签在哪里core。现在,当您单击按钮时,浏览器会将您的<input>元素序列化为url 编码的表单参数并发送它们。它们将作为服务器/Web 应用程序的请求参数出现。Spring 将按名称反序列化它们,并将它们作为参数注入到您有@RequestParam方法参数的地方。

回答by RekrowYnapmoc

There needs to be a form encapsulating your input.

需要有一个表单来封装您的输入。

General FYI: Your "save" and "renew" use cases should be separate controller actions.

一般仅供参考:您的“保存”和“更新”用例应该是单独的控制器操作。

Also consider removing "POST" from your action name. Seeing as the action is decorated with GET, and the html is saying its GET

还可以考虑从您的操作名称中删除“POST”。看到动作是用 GET 修饰的,而 html 说的是它的 GET