Struts2入门

目录

一、概念

二、入门

1、配置过滤器

 2、动态调用方法

3、传参

①实现ModelDriver接口

②get\set

③对象属性

 4、子控制器中获得request,response,session对象

①通过类ServletActionContext获得request,response

②实现struts提供的接口

 三、疑问


一、概念

基于MVC设计模式的web框架,Struts2作为控制器Controller来建立模型与视图的数据交互。

在三层架构中属于表示层。

二、入门

1、配置过滤器

创建的为maven项目,所以项目结构如下图

Struts2入门

 在WebContent下的web.xml中配置Struts2,在自定义mvc阶段配置的是*控制器,在这里概念

相同,一点小区别是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" version="3.1">
  <display-name>Maven_test</display-name>
   <filter>
      <filter-name>struts</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  </filter>
  <filter-mapping>
     <filter-name>struts</filter-name>
     <url-pattern>*.action</url-pattern>
  </filter-mapping>
</web-app>

org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter是如何得到的?

这个是过滤器的全路径,maven已将帮我们自动下载了Struts2的核心jar包。我们需要在prm.xml中

添加依赖让maven帮我们下载Struts2相关jar包。

<dependency>
       <groupId>org.apache.struts</groupId>
       <artifactId>struts2-core</artifactId>
       <version>2.5.13</version>
</dependency>

怎么判断jar包下载成功?在Maven Dependencies下面可以看到maven帮我们从仓库中下载的jar。

Struts2入门

 2、动态调用方法

TestAction.java

package com.zwf.web;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zwf.Po.User;

public class TestAction extends ActionSupport implements ModelDriven<User>{

	private User u = new User();
	
	private String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public User getModel() {
		return u;
	}
	
	public String add() throws Exception {
		System.out.println("add()...");
		System.out.println(u);
		return "bookEdit";
	}
	 
	public String del() throws Exception {
		System.out.println("del()...");
		System.out.println(u);
		return "bookEdit";
	}
	 
	public String edit() throws Exception {
		System.out.println("edit()...");
		System.out.println(u);
		return "bookEdit";
	}


}

index.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> 
	<h3>Struts动态调用方法</h3>
	<a href="${ pageContext.request.contextPath }/sy/demo1_add.action">新增</a><br> 
	<a href="${ pageContext.request.contextPath }/sy/demo1_edit.action">修改</a><br>
	<a href="${ pageContext.request.contextPath }/sy/demo1_del.action">删除</a> 
</body>
</html>

配置文件

Struts2入门

 struts-base.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.configuration.xml.reload" value="true" />
	<constant name="struts.i18n.reload" value="true" />
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	
	<package name="base" extends="struts-default" abstract="true">
		<global-allowed-methods>regex:.*</global-allowed-methods>
	</package>
</struts>

struts-sy.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="sy" extends="base" namespace="/sy">
	<action name="/demo1_*" class="com.zwf.web.TestAction"
			method="{1}">
			<result name="bookEdit">/Yes.jsp</result>
	</action>
	</package>
</struts>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<!-- 引入了Struts的默认配置 -->
	<include file="struts-default.xml"></include>
	<!-- 开发人员对Struts框架的基本配置 -->
	<include file="struts-base.xml"></include>
	<!-- 项目涉及到的模块,分文件管理 -->
	<include file="struts-sy.xml"></include>
	
</struts>

struts框架只会寻找Struts开头的xml文件,可以看到在Struts.xml中包含了其他两个xml文件。

这就有点类似于分类管理,Struts.xml中有注释!!

注意:之前配置的是mvc.xml文件,现在我们的项目涉及到的模块在Struts-sy.xml中配置,配置写

法发生了一点点改变。demo1_*_*

第一个*代表调用方法;

第二个*代表跳哪个界面(一般使用一个*( {1}代表第一个*)的写法);

启动服务测试!

3、传参

三种方式

①实现ModelDriver接口

②get\set

③对象属性

实体类:user.java

package com.zwf.Po;

public class User {
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}
	
}

TestAction.java

package com.zwf.web;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zwf.Po.User;

public class TestAction extends ActionSupport implements ModelDriven<User>{

	/**
	 * ModelDriver传参测试对象 
	 */
	private User u = new User();
	
	/**
	 * get\set传参测试属性
	 */
	private String uname;
	
	/**
	 * 对象传参测试对象
	 */
	private User u1;
	
	
	
	public User getU1() {
		return u1;
	}

	public void setU1(User u1) {
		this.u1 = u1;
	}

	
	
	public String getUname() {
		return uname;
	}

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

	@Override
	public User getModel() {
		return u;
	}
	
	public String add() throws Exception {
		System.out.println("ModelDriver:"+u);
		System.out.println("set&get:"+uname);
		System.out.println("对象:"+u1);
		return "bookEdit";
	}
	 
	public String del() throws Exception {
		System.out.println("ModelDriver:"+u);
		System.out.println("set&get:"+uname);
		System.out.println("对象:"+u1);
		return "bookEdit";
	}
	 
	public String edit() throws Exception {
		System.out.println("ModelDriver:"+u);
		System.out.println("set&get:"+uname);
		System.out.println("对象:"+u1);
		return "bookEdit";
	}


}

index.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> 
	<h3>Struts传参</h3>
	<a href="${ pageContext.request.contextPath }/sy/demo1_add.action?name=zs&age=11">ModelDriver传参数</a><br> 
	<a href="${ pageContext.request.contextPath }/sy/demo1_edit.action?uname=ls">get\set</a><br>
	<a href="${ pageContext.request.contextPath }/sy/demo1_del.action?u1.name=zs&u1.age=12">对象</a> 
</body>
</html>

从上往下点击一次测试,打印结果

Struts2入门

 4、子控制器中获得request,response,session对象

①通过类ServletActionContext获得request,response

②实现struts提供的接口

package com.zwf.web;

import javax.servlet.ServletRequest;
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 com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zwf.Po.User;

public class TestAction extends ActionSupport implements ModelDriven<User>, ServletRequestAware, ServletResponseAware {

	private HttpServletRequest req;
	private HttpServletResponse resp;

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

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}
    
    public String add() throws Exception {
		req.setAttribute("test", "测试成功");
		return "bookEdit";
	}

}

测试结果如下图:

Struts2入门

 三、疑问

通过阅读上文可以知道传参有三种方式,如果get\set方式传参的参数名和ModelDriver驱动的实体

类的其中一个属性名相同!!!那么这时候前端传过来的参数到底会赋值给哪一个呢?请看下篇

ognl讲解!

上一篇:mysql 按表达式或函数分组、多个字段分组、排序


下一篇:linux-php网站服务器防流量外出攻击(转)