struts简介
Struts是最早的Java开源框架之一,它是MVC设计模式的一个优秀实现。 Struts定义了通用的Controller(控制器),通过配置文件(通常是 Struts -config.xml)
Struts是最早的java开源框架之一,它是MVC设计模式的一个优秀实现。 Struts定义了通用的Controller(控制器),通过配置文件(通常是 Struts -config.xml)隔离Model(模型)和View(视图),以Action的概念以对用户请求进行了封装,使代码更加清晰易读。 Struts还提供了自动将请求的数据填充到对象中以及页面标签等简化编码的工具。 Struts能够开发大型Java Web项目。
1. 环境搭建
搭建环境需要联系Maven【一】中介绍的相关知识
1.1 jar
引入以下代码进行导包:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
1.2 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>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
如图所示:
1.3 struts.xml
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>
<include file="struts-default.xml"></include><!-- 默认模块 -->
<include file="struts-base.xml"></include><!-- 基础模块 -->
<include file="struts-sy.xml"></include><!-- 系统 模块-->
</struts>
如图所示:
- 开发
我们以我们学过的mvc框架的思想来理解它
2.1 Action
2.1.1 不需要指定父类(ActionSupport)
2.1.2 业务方法的定义
public String xxx();//execute
HolleAction.java
相当于我们mvc框架中的子控制器,但是mvc需要传一个methondName的参数,在现在不需要传了
代码如下:
package com.dengrenli;
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.ModelDriven;
/**
* 1.动态方法调用(mvc框架中不具备的优势)
* 2.struts中的传参
* 3.strets与tomcat的交互
* 如何将后台的值传到前台去
* 1.request传值
* 2.值栈传值(get反法传值)
* @author VULCAN
*
*/
public class HolleAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
private User user1=new User();
private User user2;
private String sex;
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() {
System.out.println("add方法");
return "success";
}
public String del() {
System.out.println("del方法");
return "success";
}
public String edit() {
System.out.println("edit方法");
return "success";
}
public String list() {
System.out.println("list方法");
System.out.println("user1:"+user1);
System.out.println("user2:"+user2);
System.out.println("sex:"+sex);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("rs",user1);
// this.request.setAttribute("rs", user1);
return "success";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
/*@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}*/
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
}
}
struts-sy.xml
相当于我们mvc当中的mvc.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="/hello_*" class="com.dengrenli.HolleAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
如图所示:
success.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}
</body>
</html>
动态方法的调用
这里与mvc框架就很有区别了,mvc需要传一个methondName的参数,才能识别它需要的是哪个方法,在struts框架中我们只需要在访问该网址的时候就把我们所需要的方法拿到了。
例如:http://localhost:8080/T226_struts2/sy/hello_list.action中我们的list指的就是我们的HolleAction.java中的list方法,所以我可以修改成del的方法名,这样就可以调用于之对应的方法了 2.2 参数赋值
2.2.1 Action中定义属性,并提供get/set方法
2.2.2 ModelDriven
返回实体,不能为null,不需要提供get/set方法
2.2.3 ModelDriven返回实体和Action中属性重名,ModelDriven中优先级更高
注:ognl,ActionContext学完就知道了
HolleAction.java
代码如下:
package com.dengrenli;
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.ModelDriven;
/**
* 1.动态方法调用(mvc框架中不具备的优势)
* 2.struts中的传参
* 3.strets与tomcat的交互
* 如何将后台的值传到前台去
* 1.request传值
* 2.值栈传值(get反法传值)
* @author VULCAN
*
*/
public class HolleAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
private User user1=new User();
private User user2;
private String sex;
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() {
System.out.println("add方法");
return "success";
}
public String del() {
System.out.println("del方法");
return "success";
}
public String edit() {
System.out.println("edit方法");
return "success";
}
public String list() {
System.out.println("list方法");
System.out.println("user1:"+user1);
System.out.println("user2:"+user2);
System.out.println("sex:"+sex);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("rs",user1);
// this.request.setAttribute("rs", user1);
return "success";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
/*@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}*/
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
}
}
User.java
代码如下:
package com.dengrenli;
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 + "]";
}
public User(String uid, String uname) {
this.uid = uid;
this.uname = uname;
}
public User() {}
}
Dome.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>
<h2>struts传参的三种方式</h2>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?uid=001&&uname=zs">测试ModelDriven接口传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?sex=nan">测试set传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?user2.uid=002&&user2.uname=ls">测试参数名.属性名传参</a>
<h2>与J2EE容器交互</h2>
</body>
</html>
结果如图所示:
2.3 与J2EE容器交互
有两种方式
第一种:
1.request传值
1.1 非注入
1.1.2 耦合
非注入就是使用 HttpServletRequest request = ServletActionContext.getRequest();
代码如下:
HolleAction.java
代码如下:
package com.dengrenli;
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.ModelDriven;
/**
* 1.动态方法调用(mvc框架中不具备的优势)
* 2.struts中的传参
* 3.strets与tomcat的交互
* 如何将后台的值传到前台去
* 1.request传值
* 2.值栈传值(get反法传值)
* @author VULCAN
*
*/
public class HolleAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
private User user1=new User();
private User user2;
private String sex;
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() {
System.out.println("add方法");
return "success";
}
public String del() {
System.out.println("del方法");
return "success";
}
public String edit() {
System.out.println("edit方法");
return "success";
}
public String list() {
System.out.println("list方法");
System.out.println("user1:"+user1);
System.out.println("user2:"+user2);
System.out.println("sex:"+sex);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("rs",user1);
// this.request.setAttribute("rs", user1);
return "success";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
/*@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}*/
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
}
}
success.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}
</body>
</html>
效果如图所示:
1.1.2 解耦(建立使用解耦模式)(一般不用,所以有兴趣的可以去了解一下)
ActionContext
1.2 注入
1.2.1 耦合
先实现模型驱动接口ServletRequestAware,然后实现setServletResponse方法,如果在方法的开头部写上private HttpServletRequest request;然后在使用this.request.setAttribute(“rs”, user1);来进行传值
HolleAction.java
代码如下:
package com.dengrenli;
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.ModelDriven;
/**
* 1.动态方法调用(mvc框架中不具备的优势)
* 2.struts中的传参
* 3.strets与tomcat的交互
* 如何将后台的值传到前台去
* 1.request传值
* 2.值栈传值(get反法传值)
* @author VULCAN
*
*/
public class HolleAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
private User user1=new User();
private User user2;
private String sex;
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() {
System.out.println("add方法");
return "success";
}
public String del() {
System.out.println("del方法");
return "success";
}
public String edit() {
System.out.println("edit方法");
return "success";
}
public String list() {
System.out.println("list方法");
System.out.println("user1:"+user1);
System.out.println("user2:"+user2);
System.out.println("sex:"+sex);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("rs",user1);
// this.request.setAttribute("rs", user1);
return "success";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
/*@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}*/
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
}
}
success.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}
</body>
</html>
效果如图所示:
1.2.2 解耦(一般不用,所以有兴趣的可以去了解一下)
第二种:
2.值栈传值(get反法传值),只要有get方法的,它就会将传过来的值自己赋值
HolleAction.java
代码如下:
package com.dengrenli;
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.ModelDriven;
/**
* 1.动态方法调用(mvc框架中不具备的优势)
* 2.struts中的传参
* 3.strets与tomcat的交互
* 如何将后台的值传到前台去
* 1.request传值
* 2.值栈传值(get反法传值)
* @author VULCAN
*
*/
public class HolleAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest request;
private HttpServletResponse response;
private User user1=new User();
private User user2;
private String sex;
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() {
System.out.println("add方法");
return "success";
}
public String del() {
System.out.println("del方法");
return "success";
}
public String edit() {
System.out.println("edit方法");
return "success";
}
public String list() {
System.out.println("list方法");
System.out.println("user1:"+user1);
System.out.println("user2:"+user2);
System.out.println("sex:"+sex);
// HttpServletRequest request = ServletActionContext.getRequest();
// request.setAttribute("rs",user1);
// this.request.setAttribute("rs", user1);
return "success";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
/*@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}*/
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
}
}
上述代码中我们没有使用request传值 方式,但我们可以看到我们的user2有get方法,所以我直接可以在success.jsp中
success.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>
成功页面!${user2}
</body>
</html>
结果如图所示:
谢谢大家,多多指教!!!