Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。
一、 Struts2的环境搭建
1.在maven项目的pom.xml中引入Struts的依赖
<dependency> 2 <groupId>org.apache.struts</groupId> 3 <artifactId>struts2-core</artifactId> 4 <version>2.5.13</version> 5 </dependency>
2.导入Struts所需的配置文件
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" /> <!-- struts2.5以后加这个配置才能开启动态方法调用 --> <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> <!-- /user/bookAction?methodName=list /sy/bookAction?methodName=list --> <package name="sy" extends="base" namespace="/sy"> <action name="/demo_*" class="com.liuwenwu.web.HelloAction" method="{1}"> <result name="rs">/rs.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>
3.配置核心过滤器 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>
二、动态方法的调用
action代码
public String add() { System.out.println("调用add方法"); return "rs"; } public String del() { System.out.println("调用del方法"); return "rs"; }
js页面代码 测试是否成功
<a href="${pageContext.request.contextPath }/sy/demo_add.action">新增</a> <a href="${pageContext.request.contextPath }/sy/demo_del.action">删除</a>
控制台有相应的输出就是成功了
三、struts前台传递参数到后台
三种方法:
1.implements modelDrivern
2.类实例.属性名
3.set/get
先建立一个Cal实体类
package com.liuwenwu.entity; public class Cal { private String num1; private String num2; public String getNum1() { return num1; } public void setNum1(String num1) { this.num1 = num1; } public String getNum2() { return num2; } public void setNum2(String num2) { this.num2 = num2; } @Override public String toString() { return "Cal [num1=" + num1 + ", num2=" + num2 + "]"; } }
1.实现modelDrivern
public class HelloAction implements ModelDriven<Cal>,ServletRequestAware{ private Cal cal1=new Cal(); @Override public Cal getModel() { return cal1; } /** * implements modelDrivern 接收参数值 * @return */ public String accept1() { System.out.println("cal1:"+cal1); return "rs"; } }
jsp页面代码
<a href="${pageContext.request.contextPath }/sy/demo_accept1.action?num1=20&&num2=5">accept1</a>
2.类实例.属性名
private Cal cal2; public Cal getCal2() { return cal2; } public void setCal2(Cal cal2) { this.cal2 = cal2; } /** * 类实例.属性名 接收参数值 * @return */ public String accept2() { System.out.println("cal2:"+cal2); return "rs"; }
jsp页面代码
<a href="${pageContext.request.contextPath }/sy/demo_accept2.action?cal2.num1=20&&cal2.num2=5">accept2</a>
3.set/get
private String sex; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } /** * set/get 接收参数值 * @return */ public String accept3() { System.out.println(sex); return "rs"; }
jsp页面代码
<a href="${pageContext.request.contextPath }/sy/demo_accept3.action?sex=nv">accept3</a>
四、后台传值到前台
set/get定义的属性是可以接受到的
req.set.....
1.注入方式(耦合):实现ServletRequestAware接口
public class HelloAction implements ModelDriven<Cal>,ServletRequestAware{ //注入耦合 private HttpServletRequest req; /** * implements modelDrivern 接收参数值 * @return */ public String accept1() { System.out.println("cal1:"+cal1); //注入耦合 req.setAttribute("cal1", cal1); return "rs"; } @Override public void setServletRequest(HttpServletRequest req) { this.req=req; } }
2.非注入(耦合)
/** * implements modelDrivern 接收参数值 * @return */ public String accept1() { System.out.println("cal1:"+cal1); //非注入耦合 HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("cal1", cal1); return "rs"; }