在Spring Controller中动态更改表单操作名称

时间:2017-09-26 14:06:50

标签: java html spring

您好我正试图在浏览器中点击不同的网址时更改spring控制器中的表单操作。

当我点击网址时:http://localhost:8080/DEMO/shas/getExtLogin?key=11然后操作会在表单标记中追加为action =“/ DEMO / shas / getExtLogin?key = 11”。

但是我需要在LoginController.java getExternalLogin()方法中将action更改为action =“/ DEMO / admin /”,当遇到url:http://localhost:8080/DEMO/shas/getExtLogin?key=11

我的jsp代码:login.jsp

<form:form id="login" commandName="loginDO" clas="form-header">

我的Java代码:LoginController.java

    @RequestMapping(value = "/getExtLogin", method = RequestMethod.GET)
    public ModelAndView getExternalLogin(HttpServletRequest request) {
        String extInd = request.getParameter("extInd");
        request.getSession().setAttribute("extInd", extInd);
        return new ModelAndView("jsp/login").addObject("loginDO", new LoginDO());

    }

在返回ModelAndView时,有没有办法在spring控制器中更改表单操作?

1 个答案:

答案 0 :(得分:0)

我已经通过在表单标记中添加action =“$ {addUrl}”属性来解决。

<form:form id="login" commandName="loginDO" action="${addUrl}" clas="form-header">

并修改了以下方法。

@RequestMapping(value = "/getExtLogin", method = RequestMethod.GET)
public ModelAndView getExternalLogin(HttpServletRequest request) {

    String extInd = request.getParameter("extInd");
    request.getSession().setAttribute("extInd", extInd);

    ModelAndView mav = new ModelAndView();
    mav.addObject("addUrl", "/DEMO/admin/");
    mav.addObject("loginDO", new LoginDO());
    mav.setViewName("auth/crclogin");
    return mav;
}