Change page title from Spring MVC Velocity view?

瘦欲@ 提交于 2019-12-12 04:05:47

问题


I feel like page title ought to be defined by the view rather than by a controller or model.

In Zend Framework, I could write this in the view: $this->headTitle('Signup');

And that would change the page's window title to 'Signup'.

How can I do that in Java Spring MVC using Velocity for the view?

I thought maybe I could use something like:

$page.setTitle("Signup")

but it didn't work.

This is probably similar to this question: https://stackoverflow.com/questions/18539645/how-to-set-head-meta-tag-from-view-layer-in-spring-mvc-velocity

P.S. I'm also using Apache Tiles, so I have a Velocity file for layout.vm (which creates the HTML, HEAD, BODY, etc) and a Velocity file for signup.vm (which just creates the signup form). I want to be able to specify within signup.vm what the page's title should be.


回答1:


Your velocity view is generating all of the HTML that is sent to the browser. You just add a <title> element to your page.

SignupController.java

@RequestMapping(value = "/signUp", method = RequestMethod.GET)
public ModelAndView signup() {
    ModelAndView modelView = new ModelAndView("sign_up");
    modelView.setObect("personName", "The Dude");
    return modelView;
}

sign_up.vm

<html>
  <head>
    <title>Signup</title>
  </head>
  <body>
    <p>Looks like you are signing up for something, ${personName}</p>
  </body>
</html>


来源:https://stackoverflow.com/questions/18539517/change-page-title-from-spring-mvc-velocity-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!