Struts2从请求里面提取信息

本文详细介绍了如何在Struts2框架下实现登录Action,并通过ValueStack获取请求参数的过程。包括从请求中获取参数的多种方法,如通过ActionContext、ValueStack、请求对象等,并对比了不同获取方式的异同。此外,还展示了如何在ValueStack中压入对象并获取其属性值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package web; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.apache.struts2.StrutsConstants; import org.apache.struts2.StrutsStatics; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; public class LoginAction implements Action { private String userName; /** * 该方法会被Struts2拦截执行以将请求参数绑定到Action属性 * * @param userName */ public void setUserName(String userName) { this.userName = userName; } /** * 该方法会被ValueStack方法调用以获得ognl表达式指定的名为userName的属性的值 * * @return */ public String getUserName() { return userName; } public String doLogin() { // 从请求里面获取参数方法一 System.out .println("---------------------------------从请求里面获取参数一【ActionContext.getContext().getParameters().get()】-------------------------------"); Map<String, Object> req = (Map<String, Object>) ActionContext .getContext().getParameters(); Object[] v1 = (Object[]) req.get("userName"); if (v1 != null && v1.length > 0) { System.out.println("使用ActionContext获取:" + v1[0]); } // 从请求里面获取参数二 System.out .println("---------------------------------从请求里面获取参数二【ActionContext.getContext().getValueStack().findValue()】-------------------------------"); ValueStack vStatck = ActionContext.getContext().getValueStack(); // 下面的findValue方法会从ValueStack中寻找root对象集合中的哪个对象拥有getUserName()方法,如果有该方法则执行该方法获得返回值 Object ognl = vStatck.findValue("userName"); System.out.println("使用ValueStack获取:" + ognl); // 从请求里面获取参数三 System.out .println("---------------------------------从请求里面获取参数三【ActionContext.getContext().get(\"request\").get()】-------------------------------"); Map<String, Object> map = (Map<String, Object>) ActionContext .getContext().get("request"); // 下面的get方法会从ValueStack中寻找root对象集合中的哪个对象拥有getUserName()方法,如果有该方法则执行该方法获得返回值, // 从上面两种方式可以看出使用ActionContext.getContext().get("request")得到的Map取得的值和ValueStack取的值是一样的。 System.out.println("come from Maprequest:" + map.get("userName")); // 从请求里面获取参数四 System.out .println("---------------------------------从请求里面获取参数四【ServletActionContext.getRequest().getParameterMap().get()[0]】-------------------------------"); HttpServletRequest request = ServletActionContext.getRequest(); Map<String, Object[]> req2 = request.getParameterMap(); if (req2.get("userName") != null) System.out.println("come from orig request:" + req2.get("userName")[0]); // 从请求里面获得参数五 System.out .println("--------------------------------从请求里面获取参数五【ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST).get()[0]】"); request = (HttpServletRequest) ActionContext.getContext().get( StrutsStatics.HTTP_REQUEST); req2 = request.getParameterMap(); if (req2.get("userName") != null) System.out.println("come from orig request:" + req2.get("userName")[0]); // 向ValueStack中压入一个对象到栈顶 Employee emp = new Employee() { { setUserName("tong"); } }; ActionContext.getContext().getValueStack().push(emp); // 向ValueStack中压入一个对象到栈顶,从请求里面获取参数 System.out .println("---------------------------------向ValueStack中压入一个对象到栈顶,再次取参数【ActionContext.getContext().getValueStack().findValue()】-------------------------------"); vStatck = ActionContext.getContext().getValueStack(); ognl = vStatck.findValue("userName");// 这里会从ValueStack中寻找root对象中的对象是否拥有getUserName()方法,如果有该方法则执行该方法获得返回值 System.out.println("使用ValueStack获取:" + ognl); return Action.SUCCESS; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return null; } } class Employee { private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }


测试url:

http://localhost:8080/strutsdemo/doLogin.action?userName=jiang

控制台输出:

---------------------------------从请求里面获取参数一【ActionContext.getContext().getParameters().get()】-------------------------------
使用ActionContext获取:jiang
---------------------------------从请求里面获取参数二【ActionContext.getContext().getValueStack().findValue()】-------------------------------
使用ValueStack获取:jiang
---------------------------------从请求里面获取参数三【ActionContext.getContext().get("request").get()】-------------------------------
come from Maprequest:jiang
---------------------------------从请求里面获取参数四【ServletActionContext.getRequest().getParameterMap().get()[0]】-------------------------------
come from orig request:jiang
--------------------------------从请求里面获取参数五【ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST).get()[0]】
come from orig request:jiang
---------------------------------向ValueStack中压入一个对象到栈顶,再次取参数【ActionContext.getContext().getValueStack().findValue()】-------------------------------
使用ValueStack获取:tong

我们将LoginAction中的getUserName方法更改为

/** * 该方法会被ValueStack方法调用以获得ognl表达式指定的名为userName的属性的值 * * @return */ public String getMyUserName() { return userName; }


刷新浏览器再次输出:

---------------------------------从请求里面获取参数一【ActionContext.getContext().getParameters().get()】-------------------------------
使用ActionContext获取:jiang
---------------------------------从请求里面获取参数二【ActionContext.getContext().getValueStack().findValue()】-------------------------------
使用ValueStack获取:null
---------------------------------从请求里面获取参数三【ActionContext.getContext().get("request").get()】-------------------------------
come from Maprequest:null
---------------------------------从请求里面获取参数四【ServletActionContext.getRequest().getParameterMap().get()[0]】-------------------------------
come from orig request:jiang
--------------------------------从请求里面获取参数五【ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST).get()[0]】
come from orig request:jiang
---------------------------------向ValueStack中压入一个对象到栈顶,再次取参数【ActionContext.getContext().getValueStack().findValue()】-------------------------------
使用ValueStack获取:tong

我们可以看出ValueStack已经不能在栈中找到某个拥有getUserName方法的对象,所以输出值为null。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值