struts

一:环境搭配

Struts的环境搭建基于Maven

在项目中的pom.xml加入struts:http://maven.apache.org/download.cgi

struts

 二:导入配置文件

 struts

三:配置xml

struts

 

user

package com.zl;

public class User {

    private String uid;
    private String uname;
    public String getUid() {
        return uid;
    }
    public void setUid(String uid) {
        this.uid = uid;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    @Override
    public String toString() {
        return "User [uid=" + uid + ", uname=" + uname + "]";
    }
    public User(String uid, String uname) {
        super();
        this.uid = uid;
        this.uname = uname;
    }
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    
}

  

HelloAction 
package com.zl.web;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.omg.PortableInterceptor.SUCCESSFUL;

import com.zl.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 1.动态方法调用(MVC不具备的优势) 
 * 2.Struts中的参数 
 *  1.set传参
 *  2.参数名属性名传参 
 *  3实现modeldriven接口传参
 * 3.struts与tomacat的交互 如何将后台的值传到前台去 
 *  1.request传值
 * 
 * 
 *
 */
public class HelloAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware {

    private HttpServletRequest request;
    private HttpServletResponse response;
    private User user1 = new User();
    private User user2;
    private String sex;
    private String uname;
    
    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public User getUser2() {
        return user2;
    }

    public void setUser2(User user2) {
        this.user2 = user2;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String add() {
        System.out.println("add方法");
        return "success";
    }

    public String del() {
        System.out.println("del方法");
        return "success";
    }

    public String edit() {
        System.out.println("edit方法");
        return "success";
    }

    public String list() {
        System.out.println("list方法");
        System.out.println("user1:" + user1);
        System.out.println("user2:" + user2);
        System.out.println("sex:" + sex);
        System.out.println("uname:"+uname);
//        HttpServletRequest request= ServletActionContext.getRequest();
//        request.setAttribute("rs",user1);
        this.request.setAttribute("rs", user1);
        return "success";
    }

    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user1;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request=request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response=response;
    }

}

  

jsp页面测试

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<h2>struts传参的三种方式</h2>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?uid=001&&uname=zs">测试modeldriven接口传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?sex=nv">测试set接口传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?user2.uid=002&&user2.uname=ls">测试参数名.属性名接口传参</a>
<h2>与J2EE容器的交互</h2>
</body>
</html>

  

jsp页面接受结果

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
成功页面:${rs }
</body>
</html>

  

 

注入与非注入

 注入,注入需要实现ServletRequestAware接口,重写需要实现的方法setServletRequest;

  将setServletRequest方法的参数私有化作为属性

package com.zl.web;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class HelloAction implements ModelDriven<User>,ServletRequestAware{
/**
 * 1.动态方法调用(mvc不具备的优势)
 * 
 * 2.struts中的传参
 *  2.1 set 传参
 *  2.2 参数名点属性名
 *  2.3实现modeldriven接口传参
 *  
 * 3.struts 与tomcat的交互
 * 3.1如何将后台的值传到前台去
 * 1 req 传值
 * 2 值栈传值(get方法)
 * @return
 */
	private HttpServletRequest requst;
	
	private User user1=new User();
	private User user2;
	private String sex;
	
	public User getUser2() {
		return user2;
	}
	public void setUser2(User user2) {
		this.user2 = user2;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String add() {
		System.out.println("add。。。。");
		return "success";
	}
	public String del() {
		System.out.println("del。。。。");
		return "success";
	}
	public String update() {
		System.out.println("update。。。。");
		return "success";
	}
	public String list() {
		System.out.println("list。。。。");
		System.out.println("user1:"+user1);
		System.out.println("user2:"+user2);
		System.out.println("sex:"+sex);
		
//		HttpServletRequest request=ServletActionContext.getRequest();
//		request.setAttribute("rs",user1);
		this.requst.setAttribute("rs", user1);
		
		return "success";
	}
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user1;
	}
	@Override
	public void setServletRequest(HttpServletRequest requst) {
		// TODO Auto-generated method stub
		this.requst=requst;
		
	}
	
	
}

  

 非注入

package com.zl.web;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class HelloAction implements ModelDriven<User>,ServletRequestAware{
/**
 * 1.动态方法调用(mvc不具备的优势)
 * 
 * 2.struts中的传参
 *  2.1 set 传参
 *  2.2 参数名点属性名
 *  2.3实现modeldriven接口传参
 *  
 * 3.struts 与tomcat的交互
 * 3.1如何将后台的值传到前台去
 * 1 req 传值
 * 2 值栈传值(get方法)
 * @return
 */
	private HttpServletRequest requst;
	
	private User user1=new User();
	private User user2;
	private String sex;
	
	public User getUser2() {
		return user2;
	}
	public void setUser2(User user2) {
		this.user2 = user2;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String add() {
		System.out.println("add。。。。");
		return "success";
	}
	public String del() {
		System.out.println("del。。。。");
		return "success";
	}
	public String update() {
		System.out.println("update。。。。");
		return "success";
	}
	public String list() {
		System.out.println("list。。。。");
		System.out.println("user1:"+user1);
		System.out.println("user2:"+user2);
		System.out.println("sex:"+sex);
		HttpServletRequest request=ServletActionContext.getRequest();
		request.setAttribute("rs",user1);
//		this.requst.setAttribute("rs", user1);
		
		return "success";
	}
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user1;
	}
	@Override
	public void setServletRequest(HttpServletRequest requst) {
		// TODO Auto-generated method stub
		this.requst=requst;
		
	}
	
	
}

  

 

上一篇:java期末项目实验答辩作业毕业设计工程项目源码


下一篇:struts的一些基本用法和操作