问题
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