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() %>

  

你可能感兴趣的:(struts2)