2012-12-18 5 views
0

은 (는) 쉬어야 만합니다.은 꽤 간단하지만 2 일째 나는 그것을 이해할 수 없습니다. login.jsp에 일상적인 로그인 양식이 있습니다. 필자는 본질적으로 자신에게 게시하고, 컨트롤러가 처음으로 양식에 맞았는지 또는 데이터로 제출 중인지에 따라 조치를 취하도록합니다.스프링 3 : 양식을 처리 할 수 ​​없습니다.

무엇이 일어나고있는 것은 빈 양식로드는 잘하지만, 사용자 이름과 암호 제출시 나는 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> 

이이 내 컨트롤러에 매핑이는 HTTP 얻을 :

@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

예/할 web.xml에서 스프링 디스패처 용 서블릿 매핑이 있습니다 – Lurk21

+0

매핑/* 또는 /*.jsp입니까? –

답변

0

은 어쩌면 당신은 웹 응용 프로그램 컨텍스트 루트 고려해야 할 필요가있다. webapp이 http://mydomain.com/myapp에 있다고 가정하면 just/do/login 대신/myapp/do/login에 게시해야합니다.

위의 해결 방법이 아니라면 스프링 컨텍스트.xml, web.xml, 응용 프로그램 서버 특정 구성 (tomcat context.xml/jboss-web.xml) 등을 추적해야합니다. 모든 배관 공사

+0

다른/do/매핑이 작동하고 있는지, 양식 매핑 이후가 아닐지 : – Lurk21

+0

당신이 맞았습니다. 나는 틀렸다. 다른/do/mappings는 웹 애플리케이션 컨텍스트 루트를 통해 수동으로 퍼팅 할 때만 작동했다. 애플리케이션의 컨텍스트 루트를 기본으로 수정 했으므로 이제는 문제가 없다. 감사합니다! – Lurk21

관련 문제