一.struts的配置
第一步:jar的相关依赖
<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.wmy</groupId>
<artifactId>T269_struts</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>T269_struts Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
//新添加的依赖
<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>T269_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>
第二步: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>
第三步:框架配置文件?
将struts.xml? ???struts-base.xml? ??struts-sy.xml 加到src/main/resources中
?
//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">
<action name="/demo1_*" class="com.pjl.one.web.Demo1Action" method="{1}">
<result name="bookEdit">/bookEdit.jsp</result>
</action>
<!-- <action name="/demo1_*_*" class="com.pjl.one.web.Demo1Action" method="{1}">
<result name="{2}">/{2}.jsp</result>
</action> -->
</package>
</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>
<!-- 引入了struts的默认配置 -->
<include file="struts-default.xml"></include>
<!-- 开发人员对struts框架的基本配置 -->
<include file="struts-base.xml"></include>
<!--项目涉及到的模块分文件管理 -->
<include file="struts-sy.xml"></include>
</struts>
二.struts的动态方法调用
第一种调用方式
// struts-sy.xml 中
<package name="sy" extends="base" namespace="/sy">
<action name="/demo1_*" class="com.dhm.Action.Demo1Action" method="{1}">
<result name="bookEdit">/bookEdit.jsp</result>
</action>
</package>
?新建一个页面为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>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>
</body>
</html>
第二种调用方式?
? <!-- 第一个*表示调用后台哪个方法,第二个*表示调用方法后返回哪个页面 --> ? ? ? <action name="/demo1_*_*" class="com.dhm.Action.Demo1Action" method="{1}"> ? ? ? <result name="{2}">/{2}.jsp</result> ? ? </action>
<a href="${pageContext.request.contextPath }/sy/demo1_del_bookEdit.action">删除</a>??
三.struts的传参
新建实体类
package com.wmy.one.entity;
public class User {
private String uid;
private String uname;
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 + "]";
}
}
?实现模型驱动接口
package com.wmy.one.web;
import javax.servlet.ServletResponse;
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.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wmy.one.entity.User;
public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest req;
private HttpServletResponse resp;
private User user1=new User();
private String sex;
private User user2;
public User getUser1() {
return user1;
}
public void setUser1(User user1) {
this.user1 = user1;
}
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() 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";
}
public String list() throws Exception {
System.out.println("list===");
System.out.println(user1);
System.out.println(sex);
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("age",12);
req.setAttribute("hobby","zhuiju");
return "bookEdit";
}
public User getModel() {
return user1;
}
}
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>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_list.action?sex=nv">set方法传参</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action?user1.uid=002&&uname=xp">对象属性传参1</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action?user2.uid=002&&user2.uname=xp">对象属性传参2</a><br>
</body>
</html>
展示效果:
?
四.set属性传参?
在子控制器新建一个sex
package com.wmy.one.web;
import javax.servlet.ServletResponse;
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.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wmy.one.entity.User;
public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest req;
private HttpServletResponse resp;
private User user1=new User();
private String sex;
private User user2;
public User getUser1() {
return user1;
}
public void setUser1(User user1) {
this.user1 = user1;
}
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() 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";
}
public String list() throws Exception {
System.out.println("list===");
System.out.println(user1);
System.out.println(sex);
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("age",12);
req.setAttribute("hobby","zhuiju");
return "bookEdit";
}
public User getModel() {
return user1;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.resp=response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.req=request;
}
}
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=nv">set方法传参</a><br>?
?展示效果:
五.通过对象属性传参?
新建一个对象
package com.wmy.one.entity;
public class User {
private String uid;
private String uname;
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 + "]";
}
}
?<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user1.uid=002&&uname=xp">对象属性传参1</a><br>?
?
六.struts与tomCat的集成
第一种:
?public String list() throws Exception { ? ? ? ? System.out.println("list==="); ? ? ? ? System.out.println(user1); ? ? ? ? System.out.println(sex); ? ? ? ? HttpServletRequest request=ServletActionContext.getRequest(); ? ? ? ? request.setAttribute("age",12); ? ? ? ? req.setAttribute("hobby","zhuiju"); ? ? ? ? return "bookEdit"; ? ? }
?在bookEdit中调用
<%@ 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> 成功界面====== ${age} </body> </html>
展示效果:
?
?第二种:
实现ServletRequestAware,ServletResponseAware接口
?
package com.wmy.one.web;
import javax.servlet.ServletResponse;
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.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wmy.one.entity.User;
public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest req;
private HttpServletResponse resp;
private User user1=new User();
private String sex;
private User user2;
public User getUser1() {
return user1;
}
public void setUser1(User user1) {
this.user1 = user1;
}
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() 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";
}
public String list() throws Exception {
System.out.println("list===");
System.out.println(user1);
System.out.println(sex);
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("age",12);
req.setAttribute("hobby","zhuiju");
return "bookEdit";
}
public User getModel() {
return user1;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.resp=response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.req=request;
}
}
|