2011-09-17 43 views
0

我正在使用spring mvc框架。我在頁面上有兩個提交按鈕。即將請求轉發給兩個不同的控制器。我如何在單個jsp頁面上使用兩個動作。請提出 。如何在一個jsp中使用兩個submitt並調用兩個動作

我的控制器是爲

1.

@RequestMapping(value = "/user/reset", method = RequestMethod.POST) 
     public String editUser(@ModelAttribute("users") User user, 
       BindingResult result) { 
      Integer uid=user.getId(); 
      User resetUser = usersService.findUser(uid); 
      resetUser.setActive(0); 
      ResetPasswordLog resetPasswordLog=new ResetPasswordLog(); 
      usersService.addUsers(resetUser); 
      resetPasswordLogService.setTempHash(uid); 
      String TEMPHASH= resetPasswordLog.getTempHash(); 
      System.out.println("www.lacas.com/reset?uid="+uid+"&th="+TEMPHASH); 
      return "redirect:/secure/user/" + uid; 

      } 

2.

@RequestMapping(value = "/user/edit", method = RequestMethod.POST) 
    public String addUser(@ModelAttribute("users") UserForm userForm, 
      BindingResult result) { 
     Map<String, String> map = new LinkedHashMap<String, String>(); 
     User user = usersService.findUser(userForm.getId()); 
     Integer userId = userForm.getId(); 

     User newUser = usersService.findUser(userForm.getEmail()); 
     user.setName(userForm.getName()); 
     if (newUser == null) { 
      user.setEmail(userForm.getEmail()); 
      user.getRoles().clear(); 
      Integer[] roleIds = userForm.getRoleIds(); 
      for (Integer roleId : roleIds) { 
       if (roleId != 0) { 
        Role role = roleService.findRole(roleId); 
        user.getRoles().add(role); 
       } 
      } 
      usersService.addUsers(user); 
      return "redirect:/secure/users/index"; 

     } else { 

      edit_exist_user = true; 
      return "redirect:/secure/user/" + userId; 

     } 
    } 

回答

1

您可以使用JavaScript和改變形式的action動態屬性。如果這是你的表單:

<form id="myform" action="#" onsubmit="return pickDestination();"> 
    <input type="submit" name="sbmitbtn" value="edit" onclick="document.pressed=this.value"/> 
    <input type="submit" name="sbmitbtn" value="reset" onclick="document.pressed=this.value"/> 
</form> 

然後你pickDestination JS函數看起來像:

function pickDestination() 
{ 
    var a = "/user/" + document.pressed; 
    document.getElementById("myform").action = a; 
    return true; 
} 
+0

怎麼樣?你能舉個例子嗎? – Romi

0

我會說,我不是很熟悉Spring應用前言本,但是在許多其他基於Java的MVC系統中,我通過簡單地給我的提交按鈕命名並在動作類中通過檢查請求來解析它,從而實現了這一點。

例如查找哪個提交按鈕被其參數名稱使用,調用相應的方法。以下是我偶爾使用的基於struts的解決方案的示例。如果你可以訪問你的spring控制器中的servlet請求對象,你可以做類似的事情。

@Override 
public String execute() throws Exception { 
    try { 
     // Check the request for a someone clicking the logout submit button 
     if (found("logout")) { 
      user.logout(); //invoke the logout method 
      session.remove("user"); 
      return SUCCESS; 
     } 

     // Check the request for a someone clicking the login submit button 
     if (found("login")) { 
      user.login(); 
      session.put("user", user);   
      return "login"; 
     } 

    // Catch any login exceptions 
    } catch (Exception e) { 
     user = null; 
     addActionError(e.getMessage()); 
     return INPUT; 
    } 

    return SUCCESS; 
} 

// The following method checks for a request paramater based on the key (String) 
// provided. If the key is not found or the value for the parameters is empty, it 
// returns false; 
private boolean found(String param) { 
    Object temp = request.getParameter(param); 
    if (temp != null) { 
     if (temp.toString().isEmpty()) { 
      return false; 
     } 
     return true; 
    } else { 
     return false; 
    } 
}  
相關問題