Action传参数到前台

addFieldError("name", "aaaa")//传一个键值对
addFieldError("name", "bbbb")//不是覆盖,而是再传一个值

//然后在jsp中通过struts标签拿出来

<%@taglib uri="/struts-tags"  prefix="s" %>

<s:fielderror  fieldname="name" theme="simple"/> // 这里这种标签搞出来的不仅仅是字符串,还有html样式,操。。。theme="simple"就是默认的一种简单的样式,不过可以不写,默认就是simple   -----一般不使用

用这个  <s:property value="errors.name[0]"/>
   <s:debug ></s:debug>----用这个来看堆栈信息

//用session这些东西来传

public class Action1 extends ActionSupport{

	private Map request;
	private Map session;
	private Map application;

	public String add() throws Exception {
		
		request = (Map)ActionContext.getContext().get("request");
		session = (Map)ActionContext.getContext().getSession();
		application = (Map)ActionContext.getContext().getApplication();
		
		request.put("aaa", "111");
		session.put("bbb", "222");
		application.put("ccc", "333");
		return "success";
	}
}

//jsp页面这么来取
 <s:property value="#request.aaa"/>或者用<% request.getAttribute("aaa")%>
  <s:property value="#session.bbb"/>
 <s:property value="#application.ccc"/>


//最常用的得到session,request,application的方式-----DI或叫IoC
public class Action1 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{

	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;

	public String add() throws Exception {
		
		request.put("aaa", "111");
		session.put("bbb", "222");
		application.put("ccc", "333");
		return "success";
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
		
	}

	@Override
	public void setApplication(Map<String, Object> session) {
		this.session = session;
		
	}

	@Override
	public void setSession(Map<String, Object> application) {
		this.application = application;
		
	}
}

//如果就是要拿到HttpRequest这些真实的东西,就用下面方法,但一般不用
public class Action1 extends ActionSupport {

	public Action1() {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		application = session.getServletContext();
	}

	private HttpServletRequest  request;
	private HttpSession session;
	private ServletContext application;

	public String add() throws Exception {
		
	
		return "success";
	}
}

你可能感兴趣的:(html,jsp,struts,IOC)