struts2间接访问servletApi

【例】实现添加用户功能

第一步:创建action类

package com.dwx.actions;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class AddUserAction extends ActionSupport{
	private String username;
	private String password;
	private String name;
	@Override
	public String execute() throws Exception {
		ActionContext ac=ActionContext.getContext();//获得ActionContext对象
		ac.getParameters().put("username", username);//获取HttpServletRequest对象
		ac.getSession().put("password", password);//获取HttpSession对象
		ac.getApplication().put("name", name);//获取Application对象
		return SUCCESS;
	}
	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 getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

第二步:配置struts2.xml




	
		
			/user_list.jsp
		
	

第三步:创建user_add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    添加用户信息
    
	
	
	    
	
	
	

  
  
  
  

第四步:创建user_list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    用户列表
    
	
	
	    
	
	
	

  
  
  
    
用户名 密码 真实姓名

效果:

运行用户添加页面

struts2间接访问servletApi_第1张图片

运行用户列表页面

struts2间接访问servletApi_第2张图片

你可能感兴趣的:(struts2)