一,配置Struts
1,1,新建一个maven项目,注意项目结构为:
src
main
java-->写代码的地方
resources-->放配置的地方
webapp-->放页面的地方
test
java-->写测试代码的地方
resources-->放测试代码对应配置的地方
1,1,1 修改web.xml从2.3到3.1
1,1,2 在pom.xml中改JDK版本
<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>
1,1,3 改编译器:
1,2 在pom.xml中导入依赖(导入struts与tomcat的jar包)
<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>
依赖完成
1,3 做配置
按住Ctrl+Shift+T,找到jar包的核心类,复制全路径名
<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,4 开发(写子控制器继承ActionSupport)
public class Demo1Action extends ActionSupport{
public String add() throws Exception {
System.out.println("add方法……");
return "bookEdit";
}
public String del() throws Exception {
System.out.println("del方法……");
return "bookEdit";
}
public String edit() throws Exception {
System.out.println("edit方法……");
return "bookEdit";
}
建一个跳转界面bookEdit:
struts.xml文件中选中struts-sy.xml按住Ctrl跳转到配置文件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>
<!-- 引入了struts的默认配置 -->
<include file="struts-default.xml"></include>
<!-- 开发人员对struts框架的基本配置 -->
<include file="struts-base.xml"></include>
<!-- 项目涉及到的模块,分文件管理 -->
<include file="struts-sy.xml"></include>
</struts>
进行配置
此处第一个*代表调用的方法
第二个*代表跳哪个界面
<?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.lv.web.Demo1Action" method="{1}">
<result name="bookEdit">/bookEdit.jsp</result>
</action>第二种方法:
<!-- <action name="/demo1_*_*" class="com.lv.web.Demo1Action" method="{1}">
<result name="{2}">/{2}.jsp</result>
</action> -->
</package>
</struts>
二,Struts动态方法调用
<h3>struts的动态方法调用具有一个*的调用方法</h3>
<a href="${pageContext.request.contextPath }/sy/demo1_add.action">新增</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action">删除</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_edit.action">修改</a><br><h3>struts的动态方法调用具有两个*的调用方法</h3>
<a href="${pageContext.request.contextPath }/sy/demo1_edit_bookEdit.action">修改</a><br>
执行效果:
三,Struts传参
1.ModelDriver传参
Demo1Action
public class Demo1Action extends ActionSupport implements ModelDriven<User> {
private User user1 = new User();
public String add() throws Exception {
System.out.println("add方法...");
return "bookEdit";
}
public String edit() throws Exception {
System.out.println("edit方法...");
return "bookEdit";
}
public String del() throws Exception {
System.out.println("del方法...");
return "bookEdit";
}
public String list() throws Exception {
System.out.println("list方法...");
System.out.println(user1);
return "bookEdit";
}
@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>
<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>
<br>
<h3>Struts的传参</h3>
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=zs">ModelDriver传参</a>
<br>
<a href="${pageContext.request.contextPath }/sy/demo1_edit.action">set方法传参</a>
<br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action">通过对象属性传参</a>
<br>
</body>
</html>
效果:
2.set方法传参
在Demo1Action 中建立sex属性,并建立get,set方法
在list方法中打印sex
Demo.jsp界面传值
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=zs">ModelDriver传参</a>
3.通过对象属性传参
建立private User user2;提供get,set方法
在list方法中打印user2
Demo.jsp界面传值
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=002&&uname=xb">通过对象属性传参</a>
四,Struts与Tomcat的集成
通过内置类将后台参数传到前台
public String list() throws Exception {
System.out.println("list方法...");
System.out.println(user1);
System.out.println(sex);
System.out.println(user2);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("name", "喜洋洋");
return "bookEdit";
}
在bookEdit.jsp界面接收,能拿到名字这个属性内容 “喜洋洋”