刚过去的项目中使用了一把struts2。由于搭配jsp页面前台展示而不是我习惯的velocity,因此不免要学一下struts2中的ONGL标签。。。发现用起来还是很头疼的。。经常搞不清楚%,#怎么用。
1.先解决最基础的温饱问题:如何把action中的值带到页面展示出来
(先定义一个action,有个 username字段,假设value="song"想传递到页面上)
public
class
Login
extends
ActionSupport
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private String username;
public String execute() throws Exception {
return INPUT;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public String getUsername() {
return username;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public void setUsername(String username) {
this.username = username;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
下面是页面上显示username的一些尝试:
================
直接把值显示出来。。。
=====================<
br
>
1.s:property标签 <s:property value="username"/>
2.$符号表达式 $ {username}![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
上面的两种方式都是可以显示"song"出来的,如果只用来显示,推荐 $ {username} ,简单明了。不过$ {}不可以放在struts2自带的<s:xxx > 标签中混用,会报不支持变量表达式异常。
===============
用#试一下。。。
=====================<
br
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
1.#表达式 #username
失败,页面输出"#username "
2.s:property标签1: <s:property value="#username"/>
失败,页面无输出
3.s:property标签2: <s:property value="#request.username"/>
成功,页面输出"song"(这种属性放进了request中)
4.s:property标签3: <s:property value="#session.username"/>
失败,页面无输出(没有放到session中)
=============== 用%试一下。。。=====================<br>
1.%表达式 %{username}
失败,页面输出"%{username}"
2.%表达式 %{'username'}
失败,页面输出"%{'username'}"
3.s:property标签4: <s:property value="%{username}"/>
成功,页面输出"song"
4.s:property标签Error4: <s:property value="%{'username'}"/>
失败,页面输出"username"
测试了一大把,最后总结出了4种拿到值得办法:
<s:property value="username"/>
${username}
<s:property value="#request.username"/> (#session. #application.类似)
<s:property value="%{username}"/>
未完。。。。待续
1.先解决最基础的温饱问题:如何把action中的值带到页面展示出来
(先定义一个action,有个 username字段,假设value="song"想传递到页面上)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
下面是页面上显示username的一些尝试:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
1.s:property标签 <s:property value="username"/>
2.$符号表达式 $ {username}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
上面的两种方式都是可以显示"song"出来的,如果只用来显示,推荐 $ {username} ,简单明了。不过$ {}不可以放在struts2自带的<s:xxx > 标签中混用,会报不支持变量表达式异常。
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
1.#表达式 #username
失败,页面输出"#username "
2.s:property标签1: <s:property value="#username"/>
失败,页面无输出
3.s:property标签2: <s:property value="#request.username"/>
成功,页面输出"song"(这种属性放进了request中)
4.s:property标签3: <s:property value="#session.username"/>
失败,页面无输出(没有放到session中)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
失败,页面输出"%{username}"
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
失败,页面输出"%{'username'}"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
成功,页面输出"song"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
失败,页面输出"username"
测试了一大把,最后总结出了4种拿到值得办法:
<s:property value="username"/>
${username}
<s:property value="#request.username"/> (#session. #application.类似)
<s:property value="%{username}"/>
未完。。。。待续