Java 框架 02 — Struts2_02(结果跳转方式_4种、访问原生ServletAPI的方式_3种、获得参数的方式_3种)

一、结果跳转方式

1.1 转发-页面:dispatcher

<action name="Demo4Action_*" class="c_result.Demo4Action" method="{1}" >
			<!-- 转发到jsp:dispatcher -->
			<result name="success" type="dispatcher">/success.jsp</result>
</action>

1.2 重定向-页面:redirect

<action name="Demo4Action_*" class="c_result.Demo4Action" method="{1}" >
			<!-- 重定向到jsp:redirect -->
			<result name="toRedirect" type="redirect">/success.jsp</result>
</action>

1.3 转发-Action:chain

<action name="Demo4Action_*" class="c_result.Demo4Action" method="{1}" >
			<!-- 转发到Action:chain -->
			<result name="toChain" type="chain">
				<!-- 如果转发的Action和当前属于同一个namespace,可以简写 -->
				<!-- Hello_hi -->
				<!-- 转发的完整写法 -->
	            <!-- actionName:转发到的终点Action类的类型
	            	 namespace:命名空间 -->
	            <param name="actionName">HelloAction5</param>
	            <param name="namespace">/</param>
         	</result>
</action>

1.4 重定向-Action:redirectAction

<action name="Demo4Action_*" class="c_result.Demo4Action" method="{1}" >
			<!-- 重定向到Action:redirectAction -->
    		<result name="redirectAction" type="redirectAction">
				<!-- 如果重定向的Action和当前属于同一个namespace,可以简写 -->
				
				<!-- 重定向的完整写法 -->
	            <!-- actionName:重定向到的终点Action类的类型
	            	 namespace:命名空间 -->
	            <param name="actionName">HelloAction5</param>
	            <param name="namespace">/</param>
         	</result>
</action>

二、访问原生ServletAPI的方式

  • 原理

在这里插入图片描述

2.1 通过ActionContext(数据中心)

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

// 方式一:通过ActionContext获得三种域对象
public class Demo6Action extends ActionSupport {
	@Override
	public String execute() throws Exception {
		// 获得ActionContext对象
		ActionContext context = ActionContext.getContext();
		
		// 获得application域
		Map<String, Object> application = context.getApplication();
		application.put("gendar", '女');
		
		// 获得session域
		Map<String, Object> session = context.getSession();
		session.put("age", 18);
		
		// 获得request域 -- 不建议使用
		Map<String, Object> request = (Map<String, Object>) context.get("com.opensymphony.xwork2.ActionContext.request");
		// 用ActionContext取代request域
		context.put("name", "zhangsan");
		return SUCCESS;
	}
}
  • 重点
    • 通过自己得到自己ActionContext context = ActionContext.getContext();,再得到application域对象和session域对象

2.2 通过ServletActionContext(不推荐)

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

// 方式二:获得原生的Servlet对象
public class HelloAction10 extends ActionSupport{
	public String execute() {
		// 原生request
		HttpServletRequest request = ServletActionContext.getRequest();
        
        // 原生response
        HttpServletResponse response = ServletActionContext.getResponse();
        
        // 原生session
        HttpSession session = request.getSession();
        
        //原生ServletContext
        ServletContext servletContext = ServletActionContext.getServletContext();
        
		return SUCCESS;
	}
}

2.3 通过实现接口方式

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

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

import com.opensymphony.xwork2.ActionSupport;


// 方式三:implements ServletResponseAware 
	// -- 提供成员变量
public class Demo7Action extends ActionSupport implements ServletResponseAware {
	private HttpServletRequest request;
	@Override
	public String execute() throws Exception {
		System.out.println("原生request:" + request);
		return SUCCESS;
	}

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

三、获得参数的方式

  • 表单准备:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>表单验证</title>
    </head>
    <body>
    	<form action="${pageContext.request }/HA11" method="post">
    		用户名<input type="text" name="username" placeholder="请输入用户名"><br>
    		密码<input type="password" name="password" placeholder="请输入密码"><br>
    		年龄<input type="text" name="age" placeholder="请输入年龄"><br>
    		生日<input type="date" name="birthday" ><br>
    		<input type="submit" value="提交"><br>
    	</form>
    </body>
    </html>
    

3.1 属性驱动

package e_param;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction9 extends ActionSupport  {
    // 准备与参数键名称相同的属性,生成getter/setter方法
	private String username;
	private String password;
    // 自动类型转换,只支持8大基本数据类型
	private int age;
    // 支持特定类型字符串转换成Date,如yyyy-MM-dd
	private Date birthday;
	
	public String execute() {
		System.out.println(username);
		System.out.println(password);
		System.out.println(age);
		System.out.println(birthday);
		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 int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

3.2 对象驱动

  • 准备User对象

    package e_param;
    
    import java.util.Date;
    
    public class User {
    	private String username;
    	private String password;
    	private int age;
    	private Date birthday;
    	public User() {
    		super();
    	}
    	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 int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public Date getBirthday() {
    		return birthday;
    	}
    	public void setBirthday(Date birthday) {
    		this.birthday = birthday;
    	}
    	@Override
    	public String toString() {
    		return "User [username=" + username + ", password=" + password + ", age=" + age + ", birthday=" + birthday
    				+ "]";
    	}
    }
    
  • 表单属性参数名称修改

    <body>
    	<form action="${pageContext.request }/HA10" method="post">
    		用户名<input type="text" name="user.username" placeholder="请输入用户名"><br>
    		密码<input type="password" name="user.password" placeholder="请输入密码"><br>
    		年龄<input type="text" name="user.age" placeholder="请输入年龄"><br>
    		生日<input type="date" name="user.birthday" ><br>
    		<input type="submit" name="submit" value="提交"><br>
    	</form>
    </body>
    
  • Action

    package e_param;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HelloAction10 extends ActionSupport{
    	private User user;
    	
    	public String execute() {
    		System.out.println(user);
    		return SUCCESS;
    	}
    	// 必须要有get/set方法
    	public User getUser() {
    		return user;
    	}
    	
    	public void setUser(User user) {
    		this.user = user;
    	}
    }
    

3.3 模型驱动

  • 准备user类,同上

  • Action

    package e_param;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    
    public class HelloAction11 extends ActionSupport implements ModelDriven<User>{
        // user必须初始化
    	private User user = new User();
    	@Override
    	public User getModel() {
    		return user;
    	}
    	
    	public String execute() {
    		System.out.println(user);
    		return SUCCESS;
    	}
    
    	public User getUser() {
    		return user;
    	}
    
    	public void setUser(User user) {
    		this.user = user;
    	}
    }
    
    • user必须初始化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值