2012-12-18 76 views
0

這個應該很簡單,但是第2天我弄不明白。我有一個包含例程登錄表單的login.jsp。我想基本上發佈自己,並讓控制器根據表單是第一次被觸發還是與數據一起提交來採取行動。春天3:不能處理表格

發生了什麼事是,空白表格加載罰款,但在提交用戶名和密碼的,我得到一個HTTP 404

<div id="messageBox">${loginMessage}</div> 

<form id="form1" name="form1" method="post" action="/do/login" onsubmit="return validateForm()"> 
<table> 
    <tr> 
     <td>Username:</td> 
     <td><input name="username" type="text" size=30 value="" /></td> 
    </tr> 
    <tr> 
     <td>Password:</td> 
     <td><input name="password" type="password" size=30 value="" /></td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td><input type="submit" name="submit" value="Login" /></td> 
    </tr> 
</table> 
</form> 

這些都是我的控制器的映射:

@RequestMapping(value = "/login", method=RequestMethod.POST) 
public ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password, Model model) { 
    if (username == null || password == null) { 
     // User has not specified all required fields 
     String loginMessage = "Please complete all fields"; 
     return new ModelAndView("login", "loginMessage", loginMessage); 
    } else { 
     // User has specified username and password 
     // Attempt authentication 
     Login login = new Login(); 
     isAuthenticated = login.authenticate(username, password); 

     if (isAuthenticated) { 
      // Authentication succeeded, return the options page 
      return viewOptions(model); 
     } else { 
      // Authentication failed, return the login page 
      String loginMessage = "Authentication failed"; 
      return new ModelAndView("login", "loginMessage", loginMessage); 
     } 
    } 
} 

@RequestMapping(value = "/login", method=RequestMethod.GET) 
public ModelAndView login(Model model) { 
    // Blank login screen 
    String loginMessage = "&nbsp;"; 
    return new ModelAndView("login", "loginMessage", loginMessage); 
} 

編輯後在這... 我也試過以下方法,它得到了相同的結果...

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<div id="messageBox">${loginMessage}</div> 

<form:form modelAttribute="loginForm" id="form1" name="form1" method="post" action="/do/authenticate" onsubmit="return validateForm()"> 
<table> 
    <tr> 
     <td><form:label path="username">Username:</form:label></td> 
     <td><form:input path="username" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="password">Password:</form:label></td> 
     <td><form:input path="password" /></td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td><input type="submit" name="submit" value="Login" /></td> 
    </tr> 
</table> 
</form:form> 

使用LoginForm支持對象:

package com.cloudfordev.spring3;

public class LoginForm { 

private String username; 
private String password; 

public String getUsername() { 
    return username; 
} 

public void setUsername(String username) { 
    this.username = username; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 
} 

而下面的控制器:

@Controller 
@SessionAttributes 
public class VMGeneratorController { 

@ModelAttribute("loginForm") 
public LoginForm getLoginFormObject() { 
    return new LoginForm(); 
} 

@RequestMapping(value = "/viewoptions", method = RequestMethod.GET) 
public ModelAndView viewOptions(Model model) { 

    Menu menu = new Menu(); 
    String optionsPage = menu.draw(); 
    return new ModelAndView("options", "body", optionsPage); 
} 

@RequestMapping(value = "/authenticate", method = RequestMethod.POST) 
public ModelAndView login(@ModelAttribute("loginForm") LoginForm loginForm, BindingResult result) { 
    boolean isAuthenticated = false; 

    String username = loginForm.getUsername(); 
    String password = loginForm.getPassword(); 

    if (username == null || password == null) { 
     // User has not specified all required fields 
     String loginMessage = "Please complete all fields"; 
     return new ModelAndView("login", "loginMessage", loginMessage); 
    } else { 
     // User has specified username and password 
     // Attempt authentication 
     Login login = new Login(); 
     isAuthenticated = login.authenticate(username, password); 

     if (isAuthenticated) { 
      // Authentication succeeded, return the options page 
      String loginMessage = "Success"; 
      return new ModelAndView("login", "loginMessage", loginMessage); 
     } else { 
      // Authentication failed, return the login page 
      String loginMessage = "Authentication failed"; 
      return new ModelAndView("login", "loginMessage", loginMessage); 
     } 
    } 
} 

@RequestMapping(value = "/login", method=RequestMethod.GET) 
public ModelAndView login(Model model) { 
    // Blank login screen 
    String loginMessage = "&nbsp;"; 
    return new ModelAndView("login", "loginMessage", loginMessage); 
} 
} 
+0

表單操作是/ do/login,映射是/ login。 /還有其他映射嗎?也許在類或web.xml級別? –

+0

是啊有一個servlet映射的web.xml中的彈簧調度器爲/ do – Lurk21

+0

是映射/ *還是/*.jsp? –

回答

0

也許你需要考慮到網絡的應用程序上下文根。假設您的web應用程序位於http://mydomain.com/myapp上,那麼您需要發佈到/ myapp/do /登錄,而不是僅登錄/ do/login?

如果上面是解決不了問題,那麼你需要追蹤你的春天的context.xml,web.xml中,應用服務器specifix配置(Tomcat的context.xml中/的jboss-web.xml中)等,以確保你已經GET所有水管已到位

+0

其他/ do /任何映射工作,只是不是形式後映射:( – Lurk21

+0

你是對的,但是我認爲它可能是請求映射本身的問題,但我現在編輯我原來的問題到真正的內部內容。我錯了,其他的/ do/mappings只能在我通過web-app上下文根進行手動調試時才起作用,我將我的應用程序的上下文根修正爲基礎,現在一切都很好,謝謝! – Lurk21