Struts2-配置文件result元素

作用:为动作指定结果视图

name属性:逻辑视图的名称,对应着动作方法的返回值。默认值是success

type属性:结果类型,指的就是用什么方式转到定义的页面,默认是dispatcher

result中type的取值有四种类型

dispatcher默认值使用请求转发,转向一个页面
redirect使用重定向,转向一个页面
chain转发到另一个相同名称空间的动作,转发到不同名称空间的动作
redirectAction重定向到另一个相同名称空间的动作,重定向到不同名称空间的动作

result元素 转发与重定向

dispatcher:是转发到一个页面(jsp)

chain:是转发到一个action

redirect:重定向一个页面

redirectAction:重定向到另一个action

Struts笔记2-LMLPHP

Struts笔记2-LMLPHP

自定义结果类型

上面的重定向,转发都是结果类型

结果类型就是一个类,这些类都是些com.opensymphony.xwork2.Result接口,或者继承自接口的实现类org.apache.struts2.dispatcher.StrutsResultSupport

这些类都有一个doExecute方法,用于执行结果视图,struts的内部实现就是Servlet

自定义验证码结果类型

package com.gyf.web.result;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport; import com.opensymphony.xwork2.ActionInvocation; import cn.dsna.util.images.ValidateCode; public class CAPTCHAResult extends StrutsResultSupport{
private int width;
private int height;
@Override
protected void doExecute(String arg0, ActionInvocation arg1) throws Exception {
//生成验证码
//创建一个验证码对象
ValidateCode code = new ValidateCode(width,height,4,6);
//获取response对象,因为要返回给客户端
HttpServletResponse response = ServletActionContext.getResponse();
code.write(response.getOutputStream());//write方法可以把图片写回给客户端,但是需要一个输出流
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
} }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- 配置开发模式 -->
<constant name="struts.devMode" value="true"></constant> <package name="p1" extends="struts-default" namespace="/n1">
<!--声明一个结果类型 -->
<result-types>
<result-type name="captcha" class="com.gyf.web.result.CAPTCHAResult"></result-type>
</result-types>
<!--配置action -->
<action name="checkcode">
<result type="captcha">
<param name="width">150</param>
<param name="height">60</param>
</result>
</action>
</package>
</struts>

result元素-全局视图和局部视图

Struts笔记2-LMLPHP

Struts笔记2-LMLPHP

Struts2-动作类中的servlet api 访问讲解

第一种方式

package com.gyf.web.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport{
public String test() {
//获取servlet 的api方式
//第一种方式:通过ServletActionContext
//response
HttpServletResponse response = ServletActionContext.getResponse();
//request
HttpServletRequest request = ServletActionContext.getRequest();
//session
HttpSession session = request.getSession();
//application[ServletContext]
ServletContext application = ServletActionContext.getServletContext(); System.out.println(request);
System.out.println(response);
System.out.println(session);
System.out.println(application); //NONE相当于不用跳转页面,也就是相当于不用找result标签,
return NONE;
}
}

第二种方式

package com.gyf.web.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class TestAction2 extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
HttpServletResponse response;
HttpServletRequest request;
ServletContext application;
public String test() {
//第二种方式,通过实现接口,让Struts自动注入
System.out.println(request);
System.out.println(response);
System.out.println(application); //NONE相当于不用跳转页面,也就是相当于不用找result标签,
return NONE;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response=response; }
@Override
public void setServletRequest(HttpServletRequest request) {
this.request=request; }
@Override
public void setServletContext(ServletContext application) {
this.application=application; }
}

Action接收请求参数

通过Servlet和Action的属性set方法注入获取参数

package com.gyf.web.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{
//第二种方式通过属性的set注入
private String username;
private String password; public String login() {
//第一种方式:通过ServletActionContext
//request
// HttpServletRequest request = ServletActionContext.getRequest();
// String username = request.getParameter("username");
// String password = request.getParameter("password"); System.out.println(username);
System.out.println(password);
//NONE相当于不用跳转页面,也就是相当于不用找result标签,
return NONE;
} 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;
} }

第三种方式通过Action的属性的set模型的形式注入

第四种方式通过模型驱动在action里实现一个模型驱动接口

实现步骤:

在action里实现一个模型驱动接口

提供一个模型属性,并一定要赋值

实现原理:是因为有个模型驱动的拦截器在处理,ModelDrivenIngetrceptor,处理过程中给User赋予值

package com.gyf.web.model;

public class User {
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;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
} }
<%@ 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>
<!-- 第四种方式 -->
<form action="${pageContext.request.contextPath}/login.action">
用户名<input type="text" name="username"><br>
密码<input type="password" name="password"><br>
<input type="submit" name="登录">
</form> <!-- 第三种方式 -->
<!--
<form action="${pageContext.request.contextPath}/login.action">
用户名<input type="text" name="user.username"><br>
密码<input type="password" name="user.password"><br>
<input type="submit" name="登录">
</form>
-->
</body>
</html>
package com.gyf.web.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.gyf.web.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class LoginAction extends ActionSupport implements ModelDriven<User>{ public String login() { System.out.println(user); return NONE;
}
private User user = new User();
@Override
public User getModel() { return user;
} }
05-11 10:50