【问题标题】:submit form with out spring form tags提交没有弹簧表单标签的表单
【发布时间】:2017-04-12 20:37:47
【问题描述】:

我正在 spring mvc 中开发一个简单的应用程序,而不使用 spring 表单标签,目前我的工作是通过使用下面的代码完成的,但为了理解目的,我问了这个问题。 我有两个支持 bean 类,我已绑定到一个 html <form> 标签,这在 spring 表单标签中是不可能的。

豆 1

   public class Interim {
    private int interimId;
    private BigDecimal amount;
    private int interimCategory;
    // setter n getter
    }

豆2

    public class Bcr {
    private int bcrId;
    private BigDecimal cashAmount;
    private int interimCategory;
    }

html 表单

<form action="/interim" method="get">
<input type="text" name="amount" />
<input type="text" name="cashAmount" />
<input type="text" name="interimCategory" />
<button type="submit" name="Month" > month </button>
</from>

弹簧控制器

@Controller
public TestController {

   @RequestMapping(value = "/interim", method = RequestMethod.GET)
    public String interimInit(ModelMap map) {
        map.addAttribute("interim",new Interim());
        map.addAttribute("bcr",new Bcr());
        return "interim";
    }

/// on form submit
@RequestMapping(value = "/interim", method = RequestMethod.GET, params = "Month")
    public String getMonthlyInterim(@ModelAttribute("bcr") Bcr b,ModelMap
            modelMap,@ModelAttribute("interim") Interim in) {



}

当我在两个 bean 中提交表单 spring mvc set interimCategory 时,

  1. 如何告诉spring不要设置Bcr bean的interimCategory?
  2. 这是在spring创建html表单的好方法吗?

【问题讨论】:

  • 您在表单上使用 POST 方法,但是,您的控制器上只有 GET 方法
  • 操作已编辑,谢谢 ;-)
  • 您在表单上的操作是“/test”,但您没有 RequestMethod 为“/test”的方法
  • 出于某种原因,我粘贴了所有原始代码,但我自己编写了表单
  • 你不能有 2 个具有相同路径和相同方法 (GET) 的方法

标签: java spring forms spring-mvc


【解决方案1】:

如果您不想使用 Spring 表单标签,您可以选择使用 JQuery Ajax 发送表单 + 使用 POST 方法在 HTTP 正文中发送数据,该 HTTP 消息的正文将是 JSON 消息。

然后您可以使用@RequestBody 注解将该json 绑定到控制器上的变量。

【讨论】:

  • 那我如何在getMonthlyInterim(@ModelAttribute("interim") Interim in) 方法中获得cashAmount 的值?
猜你喜欢
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 2019-06-05
  • 2012-04-19
相关资源
最近更新 更多