如何在玉兰花中完成请求映射?

时间:2015-03-30 11:18:58

标签: magnolia

这是我的以下控制器代码:

 package com.newposition.templates.components;

 import javax.jcr.Node;
 import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;

 import info.magnolia.module.blossom.annotation.Template;

 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.util.StringUtils;
 .......
 .......

@Controller
@Template(title = "Login Form", id = "newPositionModule:components/login")
public class LoginFormComponent {
@Autowired
private LoginServiceImpl loginServiceImpl;
/*@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public String defaultPage() {

    ModelAndView model = new ModelAndView();
    model.addObject("title", "Spring Security + Hibernate Example");
    model.addObject("message", "This is default page!");
    model.setViewName("home");
    return "components/loginSucess.jsp";

}*/

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String render(@ModelAttribute LoginForm loginForm,HttpServletRequest request) {
    return "components/loginForm.jsp";
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView rendering( @ModelAttribute("loginForm") @Valid LoginForm loginForm,BindingResult result,HttpServletRequest request) {
    String viewName = "";
    String user=loginServiceImpl.verifyLogin(loginForm);
      if(user!=null)
      {
          request.getSession().setAttribute("valid", true);
          request.getSession().setAttribute("user", user);
          request.getSession().setAttribute("authenticate", false);
          viewName = "components/loginSucess.jsp";
          return  new ModelAndView(viewName);
      }
      else{
          request.getSession().setAttribute("authenticate", true);
          viewName = "components/loginForm.jsp";
          return  new ModelAndView(viewName);
      }

}

}

以下Jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
 <%@ taglib
uri="http://magnolia-cms.com/taglib/templating-components/cms"
prefix="cms"%>
<%@ taglib
uri="http://magnolia-cms.com/taglib/templating-components/cmsfn"
prefix="cmsfn"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="blossom-taglib" prefix="blossom"%>


<c:if test="${authenticate}">
<b style="color:red; margin-left:560px" align="center">Please Enter a Valid Credentials</b>
 </c:if> 
<form:form id="loginform" action="?" commandName="loginForm" method="POST">
<blossom:pecid-input />
<div class="container">
    <ul class="login_box">
        <li>
            <div class="form-group">
                <form:input path="firstName" id="username" placeholder="Username"  class="form-control"/>
            </div>
            <div class="form-group">
                <form:password path="password" id="password" placeholder="Password"  class="form-control"/>
            </div>
            <div class="form-group">
                <button class="lgn_btn" type="submit" onsubmit="return validateForm()">Login</button>
            </div>
            <hr>
            <div class="form-group">
                <button class="lgn_btn" type="submit">Register as a new
                    user</button>
            </div>
            <a href='resetpassword'><button class="lgn_btn" type="button">Reset password</button></a>
        </li>
    </ul>
  </div>
</form:form>

表单提交时,请求将转到控制器发布方法之上。

在jsp中有&#34; form action =&#34;?&#34; &#34;

那么它是如何映射到特定的控制器方法的。

请建议。

1 个答案:

答案 0 :(得分:0)

我相信最近回答了同样的问题here 它将被提交到呈现表单的同一页面,并且它将最终出现在同一个Spring Controller中。