Struts框架:
-
Struts是最早的java开源框架之一,它是MVC设计模式的一个优秀实现。 Struts定义了通用的Controller(控制器),通过配置文件(通常是 Struts -config.xml)隔离Model(模型)和View(视图),以Action的概念以对用户请求进行了封装,使代码更加清晰易读。 Struts还提供了自动将请求的数据填充到对象中以及页面标签等简化编码的工具。 Struts能够开发大型Java Web项目。
-
struts2是实现的MVC模式的JavaEE企业级的轻侵入性.轻量级的表现层web开源框架,提供了开发,发布,到维护过程中的支持.
一. struts环境搭建
struts.xml(核心配置文件):
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
将jar包依赖加入pom.xml文件中:
再导入struts配置文件,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>
<!-- Struts框架自带的核心类的配置 -->
<include file="struts-default.xml"></include>
<!-- 配置Struts全局设置 -->
<include file="struts-base.xml"></include>
<!-- 将系统开发的每个模块分门别类,便于模块的action寻找 -->
<include file="struts-sy.xml"></include>
</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>
<!--
相对MVC的差异性
package:用来将一类子控制器进行分类
http://localhost:8080/T237_struts/sy/user_add.action中/sy对应的namespace="/sy"
extends,包的继承
*的含义:
*代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,*代表了add
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.cjq.one.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
<action name="demo_*" class="com.cjq.one.web.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="tomcat_*" class="com.cjq.one.web.TomcatAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
</package>
</struts>
sturts-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>
之后配置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>
二.动态方法调用;
创建UserAction控制器
package com.cjq.one.web;
import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport{
public String list() {
System.out.println("查询所有+SUCCESS");
return null;
}
public String add() {
System.out.println("增加");
return null;
}
public String del() {
System.out.println("删除");
return null;
}
public String edit() {
System.out.println("修改");
return null;
}}
在struts-sy.xml中进行配置
<action name="/user_*" class="com.cjq.one.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
测试地址:http://localhost:8080/cjq_struts1/sy/user_add.action
结果如下:
三.jsp传值到后台的三种方式:
1.set
2.实现modeldriven接口传参(自定义MVC的做法)
3.类实例.属性传参
创建一个实体类,User:
package com.cjq.one.entity;
public class User {
private String uname;
private String pwd;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [uname=" + uname + ", pwd=" + pwd + "]";
}
public User(String uname, String pwd) {
super();
this.uname = uname;
this.pwd = pwd;
}
public User() {
super();
}
}
控制器DemoAction:
package com.cjq.one.web;
import com.cjq.one.entity.User;/**
* jsp传递参数到后台的三种方式:
* 1.set
* 2.实现modeldriven接口传参(自定义MVC的做法)
* 3.类实例.属性传参
* @author cjq
*
*/public class DemoAction implements ModelDriven<User>{
private String sex;
private User user1 =new User();
private User user2;
public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} 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;
}
/**
* set传参是否成功
* @return
*/
public String test1() {
System.out.println(sex);
return "rs";
}
/**
* modeldriven接口传参是否成功
* @return
*/
public String test2() {
System.out.println(user1);
return "rs";
}
/**
* 类实例.属性传参
* @return
*/
public String test3() {
System.out.println(user2);
return "rs";
} @Override
public User getModel() {
return user1;
}}
配置struts-sy.xml:
<action name="demo_*" class="com.cjq.one.web.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
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>讲解传参的三种方式</h3>
<a href="${pageContext.request.contextPath}/sy/demo_test1.action?sex=nv">测试1</a>
<a href="${pageContext.request.contextPath}/sy/demo_test2.action?uname=zs&&pwd=123">测试2</a>
<a href="${pageContext.request.contextPath}/sy/demo_test3.action?user2.uname=ls&&user2.pwd=1234">测试3</a>
</body>
</html>
结果如下:
四.后台传值到jsp的方式(struts与tomcat的集成)
1 非注入
(1). 耦合
ServletActionContext
(2).解耦(建立使用解耦模式)
ActionContext
2 注入
(1). 耦合
找出struts2中其它的几个注入接口(XxxAware)
(2). 解耦
TomcatAction:
package com.cjq.one.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.cjq.one.entity.User;
/**
* 讲解struts与tomcat容器交互
* 1.注入
* 耦合 (使用)
* 解耦
* 2.非注入
* 耦合 (使用)
* 解耦
*
* 上面是为了获取request对象
* 1.传参可以使用request对象进行传参
* 2.struts特有的传参方式,叫做值栈传参
* 换个说法就是,只要该action有get方法,那么就能直接在结果集中获取参数值
*
* @author cjq
*
*/
public class TomcatAction implements ModelDriven<User>,ServletRequestAware/*RequestAware*/{
private String sex;
private User user1 = new User();
private User user2;
HttpServletRequest request; public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
public String demo() {
// 1.非注入 耦合
// HttpServletRequest request = ServletActionContext.getRequest();
// request.setAttribute("rs", "非注入耦合");
// 2.注入 耦合 它需要实现 ServletRequestAware
// this.request.setAttribute("rs", "注入耦合");
// 3.非注入 解耦
// ActionContext context = ServletActionContext.getContext();
//HttpServletRequest requset = (HttpServletRequest) context.get("rquest全路径名");
return "rs";
}
public User getModel() {
return null;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
//注入 解耦
/*public void setRequest(Map<String, Object> request) {
this.request= (HttpServletRequest) request.get("rquest全路径名");
}*/}
配置struts-sy.xml:
<action name="tomcat_*" class="com.cjq.one.web.TomcatAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
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>讲解传参的三种方式</h3>
<a href="${pageContext.request.contextPath}/sy/demo_test1.action?sex=nv">测试1</a>
<a href="${pageContext.request.contextPath}/sy/demo_test2.action?uname=zs&&pwd=123">测试2</a>
<a href="${pageContext.request.contextPath}/sy/demo_test3.action?user2.uname=ls&&user2.pwd=1234">测试3</a>
<h3>讲解struts与tomcat容器交互</h3>
<a href="${pageContext.request.contextPath}/sy/tomcat_demo.action">测试四</a>
<a href="${pageContext.request.contextPath}/sy/tomcat_demo.action?sex=nan">测试五</a>
<a href="${pageContext.request.contextPath}/sy/tomcat_demo.action?uname=zs&&upwd=123">测试六</a>
<a href="${pageContext.request.contextPath}/sy/tomcat_demo.action?user2.uname=ls&&user2.upwd=12">测试七</a>
</body>
</html>
结果如下:
测试四:
测试五:
测试六:
测试七: