现在的位置: 首页 > 综合 > 正文

struts数据标签的使用

2013年10月01日 ⁄ 综合 ⁄ 共 1591字 ⁄ 字号 评论关闭
 现在在做的项目页面中许多都引入了header.jsp。项目要求:需要根据登陆情况,实时的在header.jsp显示相应的链接。这里,我用到了Struts2中的数据标签,即action标签。该标签用于在JSP页面直接调用一个Action,通过指定executeResult属性参数,将Action的处理结果包含到页面中。以下是我在项目中用到的部分代码:
    引入的header.jsp页面中用到的部分代码:
<a title="<s:property value="loginMessage"/>" class="welcome" href="<s:property value="address" />"><s:property value="tip"/></a>
    对应GlobalHeaderAction中的具体逻辑:
public class GlobalHeaderAction extends ActionSupport{
private String loginName;
public String tip;
public String loginMessage;
public String address;
public String validateLogin(){
loginName = (String) ActionContext.getContext().getSession().get("userName");
Map request = (Map)ActionContext.getContext().get("request");
request.put("userName",loginName);
if(loginName!=null){
tip = "欢迎"+loginName;
loginMessage = "欢迎访问博客";
address = "blog/visitMyBlogs.action
";
return SUCCESS;
}
else
tip = "您还没登陆";
loginMessage = "请登陆";
address = "login.jsp";
return ERROR;
}

}
    Struts.xml中的具体配置:
<action name="validateLoginIn"
class="com.softeye.common.action.GlobalHeaderAction" method="validateLogin">
    <result name="success">/header.jsp</result>
    <result name="error">/header.jsp</result>
</action>
    最后,在需要引用header.jsp的页面中的相应为位置写下:
<s:action name="validateLogin" executeResult="true"></s:action>
    同时为了使功能更加符合需求,我在header.jsp页面进行了控制,部分代码如下:
<s:set name="userName" value="#request.userName"/>
      <s:if test="#userName!=null">
          <a style="color:red" href="<%=path %>/blog/vistMyBlogs">我的博客</a>                   
          [url=<%=path %>/exit]退出[/url]                      
       </s:if>
       <s:elseif test="#userName==null"> 
           <a class="nobg" href="register.jsp">注册</a>
        </s:elseif>
    至此,功能上就基本上完成了需求。

抱歉!评论已关闭.