不说废话,直接上干货


1.通过set和get传递參数

添加username 和password两个属性并添加set和get方法

package fzl.user.struts.demo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String list(){ System.out.println("list");
return "success";
}
public String input(){
System.out.println("input");
return "success";
} public String add(){ System.out.println("add");
return "success";
}}

在list使用EL表达式和struts标签调用

<pre name="code" class="html"><span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
通过EL訪问
${username }-->${password }
<h1>------------------list -----------------</h1>
通过struts标签訪问
<s:property value="username"/>--><s:property value="password"/>
</body>
</html></span>

在浏览器输入http://localhost:9000/strustDemo1/User_list?username=fzl&password=123 传入參数


另外一种方法,通过Actioncontext完毕

<span style="font-size:18px;">package fzl.user.struts.demo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String list(){
ActionContext.getContext().put("username", "flyou");
ActionContext.getContext().put("password", "553274238");
System.out.println("list");
return "success";
}
public String input(){
System.out.println("input");
return "success";
} public String add(){ System.out.println("add");
return "success";
}}</span>

list文件不用改动


第三种方法。通过servletAPI传值

<span style="font-size:18px;">package fzl.user.struts.demo;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String list(){
//ActionContext.getContext().put("username", "flyou");
//ActionContext.getContext().put("password", "553274238");
ServletActionContext.getRequest().setAttribute("username", "flyou");
ServletActionContext.getRequest().setAttribute("password", "553274238");
System.out.println("list");
return "success";
}
public String input(){
System.out.println("input");
return "success";
} public String add(){ System.out.println("add");
return "success";
}}</span>

list文件

<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
通过EL訪问
${username }-->${password }
<h1>------------------list -----------------</h1>
通过struts标签訪问
<span style="background-color: rgb(204, 0, 0);"><s:property value="#request.username"/>--><s:property value="#request.password"/></span>
</body>
</html></span>

获取的三种方式

1.通过seter和geter方法接受并传递

2.通过ActionContext.getContext().put("username", "flyou");传递參数

3.通过 ServletActionContext.getRequest.setAttribute("","")传值

最新文章

  1. iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例
  2. 基于RequireJS和JQuery的模块化编程——常见问题解析
  3. unity3d 自动保存
  4. asp存储过程
  5. Entity Framework 异常: &#39;OFFSET&#39; 附近有语法错误。\r\n在 FETCH 语句中选项 NEXT 的用法无效。
  6. webpack vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin
  7. xshell的一些常用配置
  8. Java 基本数据类型 及 == 与 equals 方法的区别
  9. 论文阅读笔记三:R2CNN:Rotational Region CNN for Orientation Robust Scene Text Detection(CVPR2017)
  10. 单源最短路——Dijkstara算法
  11. css的postion属性
  12. C#中的String类2
  13. error occurred at recursive SQL level 1
  14. 20155334 《网络攻防》 Exp9 Web安全基础
  15. pandas 合并数据
  16. Web前端开发笔试&amp;面试_02(others)
  17. Apache-commons-io包的使用及常用方法
  18. [编程] C语言的二级指针
  19. 关于angular导入第三方库的问题
  20. [GO]方法的重写

热门文章

  1. Intellij IDEA 将工程转换成maven工程 详解
  2. 小程序的tab标签实现效果
  3. Android Studio生成keystore签名文件步骤讲解
  4. R语言字符串替换
  5. css小贴士备忘录
  6. tensorflow 模型前向传播 保存ckpt tensorbard查看 ckpt转pb pb 转snpe dlc 实例
  7. c语言for循环等语句详解
  8. Maven外部依赖
  9. django学习1
  10. CI框架 -- 在视图文件中使用 PHP 替代语法