从 Spring MVC Velocity 视图更改页面标题? [英] Change page title from Spring MVC Velocity view?
问题描述
我觉得页面标题应该由视图定义,而不是由控制器或模型定义.
I feel like page title ought to be defined by the view rather than by a controller or model.
在 Zend Framework 中,我可以在视图中这样写:$this->headTitle('Signup');
In Zend Framework, I could write this in the view:
$this->headTitle('Signup');
这会将页面的窗口标题更改为注册".
And that would change the page's window title to 'Signup'.
如何在 Java Spring MVC 中使用 Velocity 作为视图?
How can I do that in Java Spring MVC using Velocity for the view?
我想也许我可以使用类似的东西:
I thought maybe I could use something like:
$page.setTitle("注册")
但是没有用.
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
附言我也在使用 Apache Tiles,所以我有一个用于 layout.vm(它创建 HTML、HEAD、BODY 等)的 Velocity 文件和一个用于 signup.vm(它只是创建注册表单)的 Velocity 文件.我希望能够在 signup.vm 中指定页面的标题应该是什么.
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.
推荐答案
您的速度视图正在生成发送到浏览器的所有 HTML.您只需在页面中添加一个
元素即可.
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>
这篇关于从 Spring MVC Velocity 视图更改页面标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!