struts2入门

一、Struts2简介

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts2是Struts的下一代产品,这个框架充分发挥了Struts1和WebWork这两种技术的优势,抛弃原来Struts1的缺点,使得Web开发更加容易。

struts2还有以下优点:

  • 项目开源,使用及扩展方便 - 天生优势; 提供Exception处理机制; Result 方式的页面导航,通过 Result
    标签很方便的实现重定向和页面跳转; 通过简单、集中的配置来调度业务类,使得配置和修改都非常容易;
    提供简单、统一的表达式语言来访问所有可供访问的数据; 提供标准、强大的验证框架和国际化框架; 提供强大的、可以有效减少页面代码的标签;
    提供良好的Ajax支持; 拥有简单的插件,只需要放入响应的 jar 包,任何人都可以扩展 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>
	<!-- 相对mvc的差异性 package:用来将一类子控制器进行分类 http://localhost:8080/T237_struts/sy/user_add.action 
		中/sy对应的namespace="/sy" extends:包的继承 *的含义: *代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,*代表了add -->
	<package name="sy" extends="base" namespace="/sy">
		<action name="/user_*" class="com.lx.one.web.UserAction"
			method="{1}">
			<result name="success">/test.jsp</result>
		</action>

		<action name="demo_*" class="com.lx.one.web.DemoAction"
	method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="tomcat_*" class="com.lx.one.web.TomcatAction"
			method="{1}">
			<result name="rs">/rs.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>
	<!-- 将系统开发的每个模块分门别类,便于模块的action寻找 -->
	<include file="struts-sy.xml"></include>
</struts>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lx</groupId>
  <artifactId>struts</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>struts Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.44</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.5.13</version>
		</dependency>
  </dependencies>
  <build>
    <finalName>struts</finalName>
    <plugins>
    <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
    </plugins>
  </build>
</project>

web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         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>Archetype Created Web Application</display-name>
  <filter>
  <!--ctrl+shift+t:查找jar也就是class的作用  -->
  <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>

以上就是struts的配置文件。

struts动态调用方法
子控制器:

package com.lx.one.web;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	public String list() {
		System.out.println("查询"+SUCCESS);
		return SUCCESS;
	}

	public String add() {
		System.out.println("增加");
		return SUCCESS;
	}

	public String del() {
		System.out.println("删除");
		return SUCCESS;
	}

	public String edit() {
		System.out.println("修改");
		return SUCCESS;
	}

}

在s’t’rustruts-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>
	<!-- 相对mvc的差异性 package:用来将一类子控制器进行分类 http://localhost:8080/T237_struts/sy/user_add.action 
		中/sy对应的namespace="/sy" extends:包的继承 *的含义: *代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,*代表了add -->
	<package name="sy" extends="base" namespace="/sy">
		<action name="/user_*" class="com.lx.one.web.UserAction"
			method="{1}">
			<result name="success">/test.jsp</result>
		</action>

		<action name="demo_*" class="com.lx.one.web.DemoAction"
	method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="tomcat_*" class="com.lx.one.web.TomcatAction"
			method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
	</package>
</struts>

然后就是test测试类:

<%@ 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>
动态方法调用成功
</body>
</html>

最后测试得到的结果:
在这里插入图片描述

jsp传递参数到后台的三种方式

user实体类:

package com.lx.one.entity;

public class User {
	private String uname;
	private String pwd;
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	@Override
	public String toString() {
		return "User [uname=" + uname + ", pwd=" + pwd + "]";
	}
	public User(String uname, String pwd) {
		super();
		this.uname = uname;
		this.pwd = pwd;
	}
	public User() {
		super();
	}
	
}

DemoAction控制器:

package com.lx.one.web;

import com.opensymphony.xwork2.ModelDriven;
import com.lx.one.entity.User;

public class DemoAction implements ModelDriven<User>{
	private String sex;
	private User user1 = new User();
	private User user2;
	
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public User getUser2() {
		return user2;
	}

	public void setUser2(User user2) {
		this.user2 = user2;
	}
	
	/**
	 * set传参是否成功
	 * @return
	 */
	public String test1() {
		System.out.println(sex);
		return "rs";
	}
	
	/**
	 * modeldriven接口传参是否成功
	 * @return
	 */
	public String test2() {
		System.out.println(user1);
		return "rs";
	}
	
	/**
	 * 类实例.属性传参是否成功
	 * @return
	 */
	public String test3() {
		System.out.println(user2);
		return "rs";
	}

	@Override
	public User getModel() {
		return user1;
	}
}

s’t’rustruts-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>
	<!-- 相对mvc的差异性 package:用来将一类子控制器进行分类 http://localhost:8080/T237_struts/sy/user_add.action 
		中/sy对应的namespace="/sy" extends:包的继承 *的含义: *代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,*代表了add -->
	<package name="sy" extends="base" namespace="/sy">
		<action name="/user_*" class="com.lx.one.web.UserAction"
			method="{1}">
			<result name="success">/test.jsp</result>
		</action>

		<action name="demo_*" class="com.lx.one.web.DemoAction"
	method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="tomcat_*" class="com.lx.one.web.TomcatAction"
			method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
	</package>
</struts>


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>讲解传参的三种方式</h3>
<a href="${pageContext.request.contextPath }/sy/demo_test1.action?sex=laoli">测试1</a>
<a href="${pageContext.request.contextPath }/sy/demo_test2.action?uname=laowang&&pwd=123">测试2</a>
<a href="${pageContext.request.contextPath }/sy/demo_test3.action?user2.uname=laoli&&user2.pwd=1234">测试3</a>


</body>
</html>

测试结果:
在这里插入图片描述

struts与tomcat容器交互
TomcatAction子控制器:

package com.lx.one.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.lx.one.entity.User;


public class TomcatAction implements ModelDriven<User>,ServletRequestAware/*RequestAware*/{
	private HttpServletRequest request;

	private String sex;
	private User user1 = new User();
	private User user2;
	
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public User getUser2() {
		return user2;
	}

	public void setUser2(User user2) {
		this.user2 = user2;
	}
	
	/**
	 * mvc:
	 * 	HttpServletRequest req,HttpServletResponse resp
	 * @return
	 */
	public String demo() {
//		HttpServletRequest request = ServletActionContext.getRequest();
//		request.setAttribute("rs", "测试非注入耦合方式");
		
//		request.setAttribute("rs", "测试注入耦合方式");
		
//		ActionContext context = ActionContext.getContext();
//		HttpServletRequest request=(HttpServletRequest) context.get("request的全路径名");
		
		return "rs";
	}

	@Override
	public User getModel() {
		return user1;
	}

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

//	@Override
//	public void setRequest(Map<String, Object> request) {
//		this.request=(HttpServletRequest) request.get("request的全路径名");
//		
//	}

}

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>
	<!-- 相对mvc的差异性 package:用来将一类子控制器进行分类 http://localhost:8080/T237_struts/sy/user_add.action 
		中/sy对应的namespace="/sy" extends:包的继承 *的含义: *代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,*代表了add -->
	<package name="sy" extends="base" namespace="/sy">
		<action name="/user_*" class="com.lx.one.web.UserAction"
			method="{1}">
			<result name="success">/test.jsp</result>
		</action>

		<action name="demo_*" class="com.lx.one.web.DemoAction"
	method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="tomcat_*" class="com.lx.one.web.TomcatAction"
			method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
	</package>
</struts>

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>讲解传参的三种方式</h3>
<a href="${pageContext.request.contextPath }/sy/demo_test1.action?sex=laoli">测试1</a>
<a href="${pageContext.request.contextPath }/sy/demo_test2.action?uname=laowang&&pwd=123">测试2</a>
<a href="${pageContext.request.contextPath }/sy/demo_test3.action?user2.uname=laoli&&user2.pwd=1234">测试3</a>

<h3>讲解struts与tomcat容器交互</h3>
<a href="${pageContext.request.contextPath }/sy/tomcat_demo.action">测试4</a>
<a href="${pageContext.request.contextPath }/sy/tomcat_demo.action?sex=nan">测试5</a>
<a href="${pageContext.request.contextPath }/sy/tomcat_demo.action?uname=xiaozhang&&pwd=123">测试6</a>
<a href="${pageContext.request.contextPath }/sy/tomcat_demo.action?user2.uname=laoli&&user2.pwd=1234">测试7</a>
</body>
</html>

测试结果:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值