使用 Spring formView 嵌套 Velocity 模板

发布于 2024-12-16 22:38:21 字数 221 浏览 3 评论 0原文

我有一个 Spring 应用程序,我想为其添加登录功能。我想将登录表单放在网站的标题中。这意味着它将包含在多个页面中。在定义表单提交到的控制器时,我应该指定什么作为 formView

是否可以将标头中包含的登录模板(包含在每个标头中:-))指定为 formView

感谢您的帮助。如果有任何不清楚的地方,我很乐意提供更多详细信息或显示代码。

I have a Spring app that I'd like to add a login feature to. I'd like to put the login form in the header of the site. This means that it'll be included on several pages. When defining the controller that the form submits to, what do I specify as the formView?

Is it possible to specify the login template that's included in header (that's included in each head :-)) as the formView?

Thanks for the help. If anything is unclear than I'm happy to provide more details or show code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2024-12-23 22:38:21

没关系。我意识到 Velocity 模板是否包含在另一个文件中并不重要。我将其添加到模板中:

<form method="POST">
#springBind("credentials.*")

我的控制器看起来像这样:

@Controller
public class SplashController implements Serializable {

    protected final Log logger = LogFactory.getLog(getClass());
    private static final long serialVersionUID = 7526471155622776147L;

    @ModelAttribute("credentials")
    public LoginCredentials getFormBean() {
        return new LoginCredentials();
    }

    @RequestMapping(method = RequestMethod.GET)
    public String showForm() {
        logger.info("In showForm method of SplashController");
        return "splash";
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView onSubmit(LoginCredentials credentials, BindingResult bindingResult) {

        logger.info("In onSubmit method of SplashController");
        logger.info("Username = " + credentials.getUsername());
        logger.info("Password = " + credentials.getPassword());
        ModelAndView modelAndView = new ModelAndView("home");
        return modelAndView;

    }

}

并且它可以工作。

Nevermind. I realized that it doesn't matter whether the Velocity template is included in another file. I added this to the template:

<form method="POST">
#springBind("credentials.*")

and my controller looks like this:

@Controller
public class SplashController implements Serializable {

    protected final Log logger = LogFactory.getLog(getClass());
    private static final long serialVersionUID = 7526471155622776147L;

    @ModelAttribute("credentials")
    public LoginCredentials getFormBean() {
        return new LoginCredentials();
    }

    @RequestMapping(method = RequestMethod.GET)
    public String showForm() {
        logger.info("In showForm method of SplashController");
        return "splash";
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView onSubmit(LoginCredentials credentials, BindingResult bindingResult) {

        logger.info("In onSubmit method of SplashController");
        logger.info("Username = " + credentials.getUsername());
        logger.info("Password = " + credentials.getPassword());
        ModelAndView modelAndView = new ModelAndView("home");
        return modelAndView;

    }

}

and it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文