• struts2 的action 向页面传值


    写一个Action类:
    public class LoginAction{
    public String execute(){
    return SUCCESS;
    }
    public void setValue(SomeBean value){
    this.value=value;
    }
    public SomeBean getValue(){
    return this.value;
    }
    private SomeBean value;
    }
    
    

      

    再写出Bean类:
    public class SomeBean{
    public String getName(){}
    public void setName(String name){}
    }
    

      

    方法一:
    使用OGNL表达式。你可以使用struts自带的标签,他们都支持OGNL,比如s:property。举个例子:
    <!--test.jsp-->
    <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <s:property value="value.name"/>
    
    你访问LoginAction的时候s:property标签就会显示getValue().getName();
    如果你想了解更多的struts2标签和OGNL表达式建议去找更详尽的资料。

      

    方法二:
    当然是使用JSP本身的性质了。通过request和session来获取值。
    我们把Action类改一下:
    public class LoginAction{
    public string execute(){
    SomeBean value=new SomeBean();
    value.setName("sfsfjsfje");
    ActionContext context=ActionContext.getContext();
    //往request里放attribute
    context.put("value",value);
    //往session里放
    context.getSession().put("value",value);
    return SUCCESS;
    }
    }
    

      

    接下来我们改页面:
    <!-- test.jsp -->
    <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%= ((SomeBean) request.getAttribute("value")).getName() %>
    <%= ((SomeBean) session.get("value")).getName() %>
    

      

  • 相关阅读:
    android 入门-Service
    android 入门-Activity及 字体
    android 入门-安装环境
    PS 零基础训练1
    wp8 入门到精通 ImageCompress 图片压缩
    wp8 入门到精通 Gallery
    C# DateTime时间格式转换为Unix时间戳格式
    wp8 入门到精通 MultiMsgPrompt
    C# Func<T,TResult>
    redis简介
  • 原文地址:https://www.cnblogs.com/lw1234/p/4571873.html
Copyright © 2020-2023  润新知