在pom.xml文件里面jar包依赖下面导入struts依赖:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
配置maven-compiler-plugin插件:
<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>
修改web.xml由2.3至3.1
<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">
添加struts2支持:
然后在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>
三个案例:
1.动态方法调用:
先建一个实体类 User:
package com.xiaoyi.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();
}
}
UserAction :
package com.xiaoyi.one.web;
import com.opensymphony.xwork2.ActionSupport;
/**1.动态方法调用
* @author Administrator
*
*/
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/struts2/sy/user_list.action
中/sy对应的namespace="/sy"
extends:包的继承
*的含义:*代表任意方法,只要前台浏览器匹配/user_*这一格式,那么user_add中,*代表add
-->
<!--1.动态调用 -->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.xiaoyi.one.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
<!-- 2.jsp传值到后台的三种方式 -->
<action name="demo_*" class="com.xiaoyi.one.web.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<!--3.后台传值到jsp的方式(struts与tomcat的集成) -->
<action name="tomact_*" class="com.xiaoyi.one.web.TomactAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
</package>
</struts>
结果:
2.jsp传值到后台的三种方式
DemoAction:
package com.xiaoyi.one.web;
import com.opensymphony.xwork2.ModelDriven;
import com.xiaoyi.one.entity.User;
/**
* 2.jsp传值到后台的三种方式:
* 1.set传参
* 2.实现modelDriven接口传参(自定义mvc的做法)
* 3.类实例.属性传参
* @author Administrator
*
*/
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;
}
}
<%@ 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>2.传参测试页面</title>
</head>
<body>
<h3>讲解传参3种方式</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=ls&&user2.pwd=1234">测试3</a>
</body>
</html>
结果:
3.后台传值到jsp的方式(struts与tomcat的集成)
TomactAction:
package com.xiaoyi.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.xiaoyi.one.entity.User;
/**3.后台传值到jsp的方式(struts与tomcat的集成)
* 讲解struts与tomact容器交互(将后台数据传到jsp)
* 1.注入(注射,没吃饭)
* 耦合 使用
* 解耦
* 2.非注入(举例:吃饭)
* 耦合 使用
* 解耦
* @author Administrator
*上面是未了获取request对象:
*1.传参可以使用request对象进行传参
*2.struts特有的传参方法:叫做值栈传参
*换个说法是:只要该action有get方法,那么他就能直接在结果集中获取参数值
*/
public class TomactAction 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
*
*
*/
public String demo() {
//非注入 耦合:
//HttpServletRequest request=ServletActionContext.getRequest();
//request.setAttribute("rs", "测试非注入耦合");
//注入耦合
request.setAttribute("rs", "测试注入耦合");
//非注入解耦方法,解耦需要写全路径名(少用,用的多的是耦合)
/*ActionContext context=ActionContext.getContext();
HttpServletRequest request= (HttpServletRequest) context.get("requst的全路径名");
*/
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("requst的全路径名");
}
*/
}
测试 demo1.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>2.传参测试页面</title>
</head>
<body>
<h3>讲解传参3种方式</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=ls&&user2.pwd=1234">测试3</a>
<h3>讲解struts与Tomact容器交互</h3>
<a href="${pageContext.request.contextPath}/sy/tomact_demo.action">测试4</a>
<!-- 只有get方法能传参 -->
<a href="${pageContext.request.contextPath}/sy/tomact_demo.action?sex=nan">测试5</a>
<a href="${pageContext.request.contextPath}/sy/tomact_demo.action?uname=haha&&pwd=123">测试6</a>
<a href="${pageContext.request.contextPath}/sy/tomact_demo.action?user2.uname=hehe&&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>
结果: