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版本
org.apache.maven.plugins maven-compiler-plugin3.7.0 1.8 1.8 UTF-8
1,1,3 改编译器:
1,2 在pom.xml中导入依赖(导入struts与tomcat的jar包)
javax.servlet
javax.servlet-api
4.0.1
provided
org.apache.struts
struts2-core
2.5.13
依赖完成
1,3 做配置
按住Ctrl+Shift+T,找到jar包的核心类,复制全路径名
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">
Archetype Created Web Application
struts
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts
*.action
框架配置文件
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
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
进行配置
此处第一个*代表调用的方法
第二个*代表跳哪个界面
二,Struts动态方法调用
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
第一种方法:
/bookEdit.jsp
第二种方法:
struts的动态方法调用具有一个*的调用方法
新增
删除
修改struts的动态方法调用具有两个*的调用方法
修改
执行效果:
三,Struts传参
1.ModelDriver传参
Demo1Action
public class Demo1Action extends ActionSupport implements ModelDriven{ 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"%>
Insert title here
struts动态方法调用
新增
修改
删除
Struts的传参
ModelDriver传参
set方法传参
通过对象属性传参
效果:
2.set方法传参
在Demo1Action 中建立sex属性,并建立get,set方法
在list方法中打印sex
Demo.jsp界面传值
ModelDriver传参
3.通过对象属性传参
建立private User user2;提供get,set方法
在list方法中打印user2
Demo.jsp界面传值
通过对象属性传参
四,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界面接收,能拿到名字这个属性内容 “喜洋洋”



