一、Struts配置
1.将web.xml2.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">
<display-name>Archetype Created Web Application</display-name>
</web-app>
2.改jdk版本
<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>zw.com</groupId>
<artifactId>Maven_01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Maven_01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>Maven_01</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>
3.改编译器
依赖:导入jar包到pom.xml中
<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>
依赖完成:
做配置:
<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>
开发:
public class TestAction 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";
}
}
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.zw.web.TestAction" method="{1}">
<result name="bookEdit">/bookEdit.jsp</result>
</action>
第二种方法:
<!-- <action name="/demo1_*_*" class="com.zw.web.TestAction" 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接口传参
public String list() throws Exception {
System.out.println("edit方法……");
System.out.println(user1);
return "bookEdit";
}
调用demo1.jsp
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=zs">Modeldriver接口传参</a><br>
2.set方法传参
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String list() throws Exception {
System.out.println("list方法……");
System.out.println(user1);
return "bookEdit";
}
调用:
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=nv">set方法传参</a><br>
3.通过对象属性传参
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=002&&uname=xh">通过对象属性传参</a>
四、Struts与Tomcat的集成
①
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("age", 12);
bookEdit文件中调用
${age }
②
private HttpServletResponse resp;
private HttpServletRequest req;
@Override
public void setServletResponse(HttpServletResponse response) {
this.resp=response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.req=request;
}