什么是Struts2?
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。
sturts的配置文件
struts 的配置是把上篇maven 环境的全部配置好的前提下把 maven 项目中的 pom.xml 加入我们 struts 的依赖,加载jar包
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
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.lyl</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>
注意:配置完了之后启动tomcat可能tomcat无法运行,如果没有代码错误的话,需要把你下载的jar包以及项目删除,重新创建项目下载jar包,tomcat无法运行是因为网络不好,下载jar包是jar包丢失了
在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>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
再导入我们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>
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">
</package>
</struts>
1、动态方法调用(mvc不具备的优势)
HelloAction
package com.lyl.web;
import com.opensymphony.xwork2.ModelDriven;
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.lyl.entity.User;
public class HelloAction{
public String add() {
System.out.println("add方法");
return "success";
}
public String del() {
System.out.println("del方法");
return "success";
}
public String edit() {
System.out.println("edit方法");
return "success";
}
public String list() {
System.out.println("list方法");
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>
<package name="sy" extends="base" namespace="/sy">
<action name="/hello_*" class="com.lyl.web.HelloAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
结果:
2、struts中的传参
2.1、set传参
2.2、参数名.属性名传参
2.3、实现modeldrivern接口传参
实体类User
package com.lyl.entity;
public class User {
private String uid;
private String uname;
public User() {
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
@Override
public String toString() {
return "User [uid=" + uid + ", uname=" + uname + "]";
}
}
HelloAction(实现接口 ModelDriven)
package com.lyl.web;
import com.opensymphony.xwork2.ModelDriven;
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.lyl.entity.User;
/**
* 1、动态方法调用(mvc不具备的优势)
* 2、struts中的传参
* 2.1、set传参
* 2.2、参数名.属性名传参
* 2.3、实现modeldrivern接口传参
* @author 2019072302
*
*/
public class HelloAction implements ModelDriven<User>{
private HttpServletRequest request;
private HttpServletResponse response;
private User user1 = new User();
private User user2 ;
private String sex;
private String uname;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String add() {
System.out.println("add方法");
return "success";
}
public String del() {
System.out.println("del方法");
return "success";
}
public String edit() {
System.out.println("edit方法");
return "success";
}
public String list() {
System.out.println("list方法");
System.out.println("user1:"+user1);
System.out.println("user2:"+user2);
System.out.println("sex:"+sex);
return "success";
}
@Override
public User getModel() {
return user1;
}
}
demo.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>
<h2>sturts传参的三种方式</h2>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?uid=001&&uname=zs">测试modeldriven接口传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?sex=nan">测试set传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?user2.uid=002&&user2.uname=ls">测试参数名.属性名传参</a>
</body>
</html>
结果:
3、struts与tomcat的交互
1、request传值
2、值栈传值(get方法传值)
HelloAction
package com.lyl.web;
import com.opensymphony.xwork2.ModelDriven;
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.lyl.entity.User;
/**
* 1、动态方法调用(mvc不具备的优势)
* 2、struts中的传参
* 2.1、set传参
* 2.2、参数名.属性名传参
* 2.3、实现modeldrivern接口传参
* 3、struts与tomcat的交互
* 如何将后台的值传到前台去
* 1、request传值
* 2、值栈传值(get方法传值)
* @author 2019072302
*
*/
public class HelloAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
private User user1 = new User();
private User user2 ;
private String sex;
private String uname;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String add() {
System.out.println("add方法");
return "success";
}
public String del() {
System.out.println("del方法");
return "success";
}
public String edit() {
System.out.println("edit方法");
return "success";
}
public String list() {
System.out.println("list方法");
System.out.println("user1:"+user1);
System.out.println("user2:"+user2);
System.out.println("sex:"+sex);
System.out.println("uname:"+uname);
HttpServletRequest request = ServletActionContext.getRequest();
//2.3.2 非注入的耦合形式
// request.setAttribute("rs", user1);
/*ActionContext context = ActionContext.getContext();
context.get("");*/
this.request.setAttribute("rs", user1);
return "success";
}
@Override
public User getModel() {
return user1;
}
//注入的耦合形式
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
/*//注入的解耦形式
@Override
public void setRequest(Map<String, Object> request) {
}*/
}
success.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 }
</body>
</html>
结果:
感谢观看!