如何从注释控制器中的表单获取值

时间:2012-07-31 15:33:11

标签: java spring jsp spring-mvc controller

我有一些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中打开管理页面时,我正在显示当前的ignoredExceptions。我需要保存ignoreExceptions的新值,用户输入和之后重定向到同一个管理页面,后者将查看ignoredExceptions的新值。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

ignoredExceptions的更改值与表单中名为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";
}

}