struts2简介
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts2是Struts的下一代产品,这个框架充分发挥了Struts1和WebWork这两种技术的优势,抛弃原来Struts1的缺点,使得Web开发更加容易。
struts2的优点:
- 项目开源,使用及扩展方便 - 天生优势;
- 提供Exception处理机制;
- Result 方式的页面导航,通过 Result 标签很方便的实现重定向和页面跳转;
- 通过简单、集中的配置来调度业务类,使得配置和修改都非常容易;
- 提供简单、统一的表达式语言来访问所有可供访问的数据;
- 提供标准、强大的验证框架和国际化框架;
- 提供强大的、可以有效减少页面代码的标签;
- 提供良好的Ajax支持;
- 拥有简单的插件,只需要放入响应的 jar 包,任何人都可以扩展 Struts2 框架。
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/stauts/sy/user_add.action
中/sy对应的namespace="/sy"
extends包的继承
*的含义:
代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,代表了adds
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.xhh.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
<action name="demo_*" class="com.xhh.web.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="tomcat_*" class="com.xhh.web.TomacatAction" 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>
<include file="struts-default.xml"></include>
<include file="struts-base.xml"></include>
<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.xhh</groupId>
<artifactId>stauts</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>stauts 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>stauts</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>
在Maven项目基础上加上,在pom.xml中加入struts依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
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>
<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动态调用方法
UserAction子控制器
package com.xhh.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;
}
}
在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/stauts/sy/user_add.action
中/sy对应的namespace="/sy"
extends包的继承
*的含义:
代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,代表了adds
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.xhh.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
</package>
</struts>
然后在写个test.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>
动态方法调用成功
</body>
</html>
结果:
jsp传递参数到后台的三种方式
1.set传参
2.实现modeldriven接口传参(自定义mvc的做法)
3.类实例.属性传参
User实体类
package com.xhh.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.xhh.web;
import com.opensymphony.xwork2.ModelDriven;
import com.xhh.entity.User;
/**
* jsp传递参数到后台的三种方式
* 1.set传参
*
* 2.实现modeldriven接口传参(自定义mvc的做法)
*
* 3.类实例。属性传参
* @author PC
*
*/
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;
}
/**
* ser传参是否成功
* @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";
}
public User getModel() {
return user1;
}
}
在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/stauts/sy/user_add.action
中/sy对应的namespace="/sy"
extends包的继承
*的含义:
代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,代表了adds
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.xhh.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
<action name="demo_*" class="com.xhh.web.DemoAction" 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=nv">测试1</a>
<a href="${pageContext.request.contextPath }/sy/demo_test2.action?uname=zs&&pwd=123">测试2</a>
<a href="${pageContext.request.contextPath }/sy/demo_test3.action?user2.uname=linyaodong&&user2.pwd=123">测试3</a>
</body>
</html>
结果页面:
<%@ 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>
测试:|
struts与tomcat容器交互(后台数据传到jsp)
TomacatAction控制器:
package com.xhh.web;
import java.util.Map;
import javax.servlet.ServletRequest;
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.xhh.entity.User;
/**
* struts与tomcat容器交互(后台数据传到jsp)
* 1.注入
* 耦合 使用
* 解耦
* 2.非注入
* 耦合 使用
* 解耦
*
*
* 上面是为了获取request对象
* 1.传参可以使用request对象进行传参
* 2,struts持有的传参方式我,叫做值栈传参
* 换个说法,只要该action有get方法,
* @author PC
*
*/
public class TomacatAction implements ModelDriven<User> ,ServletRequestAware {
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";
}
public User getModel() {
return user1;
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
/* @Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request=(HttpServletRequest) context.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/stauts/sy/user_add.action
中/sy对应的namespace="/sy"
extends包的继承
*的含义:
代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,代表了adds
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.xhh.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
<action name="demo_*" class="com.xhh.web.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="tomcat_*" class="com.xhh.web.TomacatAction" 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=nv">测试1</a>
<a href="${pageContext.request.contextPath }/sy/demo_test2.action?uname=zs&&pwd=123">测试2</a>
<a href="${pageContext.request.contextPath }/sy/demo_test3.action?user2.uname=linyaodong&&user2.pwd=123">测试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=zs&&pwd=123">测试6</a>
<a href="${pageContext.request.contextPath }/sy/tomcat_demo.action?user2.uname=linyaodong&&user2.pwd=123">测试7</a>
</body>
</html>
rs.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>
结果页面:${rs}
sex=${sex }
user1=${user1 }
user2=${user2 }
</body>
</html>
测试:
测试非注入耦合方式:
测试注入耦合方式:
测试struts传值: