【问题标题】:How to get value from form in an annotation controller如何从注释控制器中的表单中获取值
【发布时间】:2023-03-20 09:32:01
【问题描述】:

我有一些 jsp 页面 manage.jsp 和里面的表单:

<form method="POST" action="./manage.form">
    <div style=" float: left;">
        <textarea id="errors" name="errors">${ignoredExceptions}</textarea>
    </div>

    <div id="successOrErrorSave"></div>

    <div style="clear: both;">
        <input type="submit" id="saveIgnoredErrors" style="margin-left: auto; margin-top: 10px;" value="<spring:message code="errorlogging.ignredExceptions.save" />"/>
    </div>
 </form>

我有一些控制器 ManageController:

/**
 * The main controller.
 */
@Controller
public class ManageController {

    protected final Log log = LogFactory.getLog(getClass());


    @RequestMapping(value = "/module/manage", method = RequestMethod.GET)
    public void showForm(ModelMap model) {
        GlobalProperty glProp = Context.getAdministrationService().getGlobalPropertyObject(
            Constants.GP_IGNORED_EXCEPTION);
        if (glProp != null) {
            model.addAttribute("ignoredExceptions", glProp.getPropertyValue());
        } else {
            model.addAttribute("ignoredExceptions", "");
        }
    }


    @RequestMapping(value = "module/manage", method = RequestMethod.POST)
    public String processSubmit(@ModelAttribute(value = "ignoredExceptions") String ignoredExceprions, BindingResult result,
                                SessionStatus status) {
        boolean successSave = false;
        GlobalProperty glProp = Context.getAdministrationService().getGlobalPropertyObject(
            ErrorLoggingConstants.ERRROR_LOGGING_GP_IGNORED_EXCEPTION);
        if (glProp != null) {
            glProp.setPropertyValue(ignoredExceprions);
            GlobalProperty saved = Context.getAdministrationService().saveGlobalProperty(glProp);
            System.out.println(saved.getPropertyValue());
                        if (saved != null && saved.getPropertyValue().equals(ignoredExceprions)) {
                successSave = true;
            }
        }
                status.setComplete();
        return "module/manage";
    }
}

当我在 textarea 中打开管理页面时,我正在显示当前被忽略的异常。我需要保存用户输入的新值被忽略的异常,然后重定向到相同的管理页面,这将查看被忽略的异常的新值。我该怎么做?

【问题讨论】:

    标签: java spring jsp spring-mvc controller


    【解决方案1】:

    ignoreExceptions 的更改值与表单中名为 errors 的 textarea 字段相关联,因此您可以从请求中读取错误值或作为 RequestParameter

    @RequestMapping(value = "module/manage", method = RequestMethod.POST)
    public String processSubmit(@RequestParam(value = "errors", required = false) String   ignoredExceprions, BindingResult result,
                                SessionStatus status) {
        boolean successSave = false;
        GlobalProperty glProp = Context.getAdministrationService().getGlobalPropertyObject(
            ErrorLoggingConstants.ERRROR_LOGGING_GP_IGNORED_EXCEPTION);
        if (glProp != null) {
            glProp.setPropertyValue(ignoredExceprions);
            GlobalProperty saved = Context.getAdministrationService().saveGlobalProperty(glProp);
            System.out.println(saved.getPropertyValue());
                        if (saved != null && saved.getPropertyValue().equals(ignoredExceprions)) {
                successSave = true;
            }
        }
                status.setComplete();
        return "module/manage";
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 2012-02-14
      相关资源
      最近更新 更多