SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-004- 标签

时间:2023-05-15 11:16:32

一、<s:url>

<s:url>可以直接生成一个url或url变量,它会在href的基础上加上应用context

1.

<a href="<s:url href="/spitter/register" />">Register</a>

如果应用的context是“spittr”,则会转换为

<a href="/spittr/spitter/register">Register</a>

2.生成url变量

<s:url href="/spitter/register" var="registerUrl" />
<a href="${registerUrl}">Register</a>

3.可以指定作用域application、session、request,默认是page

<s:url href="/spitter/register" var="registerUrl" scope="request" />

4.<s:url>可以做JSTL的<c:url>不能做的,给url加参数

 <s:url href="/spittles" var="spittlesUrl">
<s:param name="max" value="60" />
<s:param name="count" value="20" />
</s:url>

5.用占位符实现 path parameter的url

<s:url href="/spitter/{username}" var="spitterUrl">
<s:param name="username" value="jbauer" />
</s:url>

参数名字若匹配不到占位符,则会变以query parameter

6.

 <s:url value="/spittles" htmlEscape="true">
<s:param name="max" value="60" />
<s:param name="count" value="20" />
</s:url>

This results in the URL being rendered like this:

/spitter/spittles?max=60&amp;count=20

7.若要在javascript中使用所定义变量

 <s:url value="/spittles" var="spittlesJSUrl" javaScriptEscape="true">
<s:param name="max" value="60" />
<s:param name="count" value="20" />
</s:url>

8.

<script>
var spittlesUrl = "${spittlesJSUrl}"
</script>

This renders the following to the response:

<script>
var spittlesUrl = "\/spitter\/spittles?max=60&count=20"
</script>

二、<s:escapeBody>

1.在html中显示"<"和">"要分别转换为&lt;和&gt;,否则会被当成html标签的结束和开始标签

<s:escapeBody htmlEscape="true">
<h1>Hello</h1>
</s:escapeBody>

This renders the following to the body of the response:

&lt;h1&gt;Hello&lt;/h1&gt;

2.The <s:escapeBody> tag also supports JavaScript escaping with the javaScriptEscape attribute:

<s:escapeBody javaScriptEscape="true">
<h1>Hello</h1>
</s:escapeBody>