struts2框架

2018/11/7

今天老师讲了struts2,本以为struts2 是struts增强版,老师介绍完struts2,才知道两者没有任何关系,

Struts 很流行耦合性比较强 代码与代码有关联

struts2 是一个自带mvc的框架,底层封装了servlet,可以理解是servlet的替代品

1.框架搭建

步骤

    1.创建web项目

    2.添加jar架包

    3.在web.xml文件中配置struts2的核心拦截器(拦截请求)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

4.手写一个Action类继承ActionSupport并重写execute方法

package com.struts2.action;

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

public class MyAction  extends ActionSupport{
private String name;
private String pwd;

	public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getPwd() {
	return pwd;
}
public void setPwd(String pwd) {
	this.pwd = pwd;
}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return "success";
	}
	public String login() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("用户名"+name);
		System.out.println("密码"+pwd);
		ActionContext.getContext().getSession().put("username", name);
		return SUCCESS;
	}


}

5.创建struts.xml 配置action

<?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" />
    
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="com.struts2.action.MyAction" method="execute">
            <result name="success">hello.jsp</result>
        </action>

        <action name="Login" class="com.struts2.action.MyAction" method="login">
            <result name="success">sucess.jsp</result>
        </action>   
        
    </package>

</struts>

 核心过滤器StrutsPrepareAndExecuteFilter

项目启动的时候会先加载web.xml文件,从而第一时间初始化核心过滤器StrutsPrepareAndExecuteFilter,这样核心过滤器就起作用了

起作用后,struts2就负责拦截所有请求

请求拦截下来之后怎么办?

当然交给配置文件 struts.xml,去struts.xml文件中找到对应的请求,找到后调用请求对应类中的对应方法来处理请求,处理玩请求后,会有一个返回值,根据返回值跳转到对应的页面,或对应的action

以上就是 请求---》拦截请求-----》处理请求---》页面跳转的整个流程

2,数据传递

1,接收页面的数据

比如:

2.获取数据库的数据

1.

2.获取name,pwd的值

3.

3,把结果传输到页面

jsp加入库标签<%@ taglib prefix="s" uri="/struts-tags" %>

一数据的传输方式三种:

  1. 属性传参  
  2. 对象传参
  3. modelDriven

二,通过三种方式访问 servletAPI

1.使用ActionContext 访问 servletAPI

使用ActionContext 提供的方法

//1.将值存于session  第一种方法获取servletAPI

ActionContext.getContext().put("username", user.getName());

ActionContext.getContext().getSession().put("username", user.getName());*/

2以ioc的方式访问servletAPI

通过实现SessionAware等接口来访问

和原始servlet API解耦合

使用了ioc优秀的编程思想

request.put("username", user.getName());

session.put("username", user.getName());

 

  • 以耦合方式访问servletAPI

使用servletActionContext提供的方法

提供的api比ActionContext多

与原始servlet API耦合,不方便扩展和调试

ServletActionContext.getContext().getSession().put("username", user.getName());

Cookie cookie=new Cookie("username", user.getName());

ServletActionContext.getResponse().addCookie(cookie);

 

使用了ioc优秀的编程思想

public class MyAction  extends ActionSupport implements RequestAware,SessionAware{

private User user;

private Map<String, Object> request;

private Map<String, Object> session;

//获取request对象并放值

@Override

public void setRequest(Map<String, Object> request) {

// TODO Auto-generated method stub

this.request=request;

 

}

//获取session对象并放值

@Override

public void setSession(Map<String, Object> session) {

// TODO Auto-generated method stub

this.session=session;

}

==============================

 

public String login() throws Exception {

request.put("username", user.getName());

session.put("username", user.getName());

return SUCCESS;

}

 

 

<!-- 只要返回值为error都跳转到fail.jsp    全局的 -->

    <global-results>

    <result name="error">fail.jsp</result>

    </global-results>      

dispatcher  转发 下方图中写错了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值