zoukankan      html  css  js  c++  java
  • struts开发<struts中的參数传递.三>

    不说废话,直接上干货



    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("","")传值


  • 相关阅读:
    http协议相关知识
    linux 常用命令总结
    PHP traits
    php 正则案例
    php 中关于正则 元字符
    【U3D】 第三人称控制器ThirdPersonCharacter添加之后角色原地打转不移动的问题(unity5.3.5f)
    .Net Core异步async/await探索
    IdentityServer4实现单点登录统一认证
    CSAPP-Tiny Web服务器【2】源码解析
    CSAPP-Tiny Web服务器【1】编译搭建
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6723720.html
Copyright © 2011-2022 走看看