Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。
一、 Struts2的环境搭建
1.在maven项目的pom.xml中引入Struts的依赖
1 <dependency> 2 2 <groupId>org.apache.struts</groupId> 3 3 <artifactId>struts2-core</artifactId> 4 4 <version>2.5.13</version> 5 5 </dependency>
2.导入Struts所需的配置文件
struts-base.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 <struts> 6 <!-- 设置编码格式 --> 7 <constant name="struts.i18n.encoding" value="UTF-8" /> 8 <!-- 开启动态方法调用 --> 9 <constant name="struts.devMode" value="true" /> 10 <!-- 修改代码在不重启项目的情况下也能立即生效 --> 11 <constant name="struts.configuration.xml.reload" value="true" /> 12 <constant name="struts.i18n.reload" value="true" /> 13 <!-- 开启动态方法调用 --> 14 <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 15 16 <!-- struts2.5以后加这个配置才能开启动态方法调用 --> 17 <package name="base" extends="struts-default" abstract="true"> 18 <global-allowed-methods>regex:.*</global-allowed-methods> 19 </package> 20 </struts>
struts-sy.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 <struts> 6 <!-- /user/bookAction?methodName=list 7 /sy/bookAction?methodName=list 8 --> 9 <package name="sy" extends="base" namespace="/sy"> 10 <action name="/demo_*" class="com.liuwenwu.web.HelloAction" method="{1}"> 11 <result name="rs">/rs.jsp</result> 12 </action> 13 </package> 14 </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
1 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 4 version="3.1"> 5 <display-name>Archetype Created Web Application</display-name> 6 <filter> 7 <filter-name>struts</filter-name> 8 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 9 </filter> 10 <filter-mapping> 11 <filter-name>struts</filter-name> 12 <url-pattern>*.action</url-pattern> 13 </filter-mapping> 14 </web-app>
二、动态方法的调用
action代码
public String add() { System.out.println("调用add方法"); return "rs"; } public String del() { System.out.println("调用del方法"); return "rs"; }
js页面代码 测试是否成功
1 <a href="${pageContext.request.contextPath }/sy/demo_add.action">新增</a> 2 3 <a href="${pageContext.request.contextPath }/sy/demo_del.action">删除</a>
控制台有相应的输出就是成功了
三、struts前台传递参数到后台
三种方法:
1.implements modelDrivern
2.类实例.属性名
3.set/get
先建立一个Cal实体类
1 package com.liuwenwu.entity; 2 3 public class Cal { 4 5 private String num1; 6 private String num2; 7 public String getNum1() { 8 return num1; 9 } 10 public void setNum1(String num1) { 11 this.num1 = num1; 12 } 13 public String getNum2() { 14 return num2; 15 } 16 public void setNum2(String num2) { 17 this.num2 = num2; 18 } 19 @Override 20 public String toString() { 21 return "Cal [num1=" + num1 + ", num2=" + num2 + "]"; 22 } 23 24 25 }
1.实现modelDrivern
1 public class HelloAction implements ModelDriven<Cal>,ServletRequestAware{ 2 private Cal cal1=new Cal(); 3 4 @Override 5 public Cal getModel() { 6 return cal1; 7 } 8 9 /** 10 * implements modelDrivern 接收参数值 11 * @return 12 */ 13 public String accept1() { 14 System.out.println("cal1:"+cal1); 15 16 return "rs"; 17 } 18 19 }
jsp页面代码
1 <a href="${pageContext.request.contextPath }/sy/demo_accept1.action?num1=20&&num2=5">accept1</a>
2.类实例.属性名
1 private Cal cal2; 2 3 public Cal getCal2() { 4 return cal2; 5 } 6 7 public void setCal2(Cal cal2) { 8 this.cal2 = cal2; 9 } 10 11 /** 12 * 类实例.属性名 接收参数值 13 * @return 14 */ 15 public String accept2() { 16 System.out.println("cal2:"+cal2); 17 return "rs"; 18 19 }
jsp页面代码
<a href="${pageContext.request.contextPath }/sy/demo_accept2.action?cal2.num1=20&&cal2.num2=5">accept2</a>
3.set/get
1 private String sex; 2 3 public String getSex() { 4 return sex; 5 } 6 7 public void setSex(String sex) { 8 this.sex = sex; 9 } 10 11 /** 12 * set/get 接收参数值 13 * @return 14 */ 15 public String accept3() { 16 System.out.println(sex); 17 return "rs"; 18 }
jsp页面代码
1 <a href="${pageContext.request.contextPath }/sy/demo_accept3.action?sex=nv">accept3</a>
四、后台传值到前台
set/get定义的属性是可以接受到的
req.set.....
1.注入方式(耦合):实现ServletRequestAware接口
1 public class HelloAction implements ModelDriven<Cal>,ServletRequestAware{ 2 //注入耦合 3 private HttpServletRequest req; 4 5 /** 6 * implements modelDrivern 接收参数值 7 * @return 8 */ 9 public String accept1() { 10 System.out.println("cal1:"+cal1); 11 //注入耦合 12 req.setAttribute("cal1", cal1); 13 return "rs"; 14 } 15 16 @Override 17 public void setServletRequest(HttpServletRequest req) { 18 this.req=req; 19 20 } 21 }
2.非注入(耦合)
1 /** 2 * implements modelDrivern 接收参数值 3 * @return 4 */ 5 public String accept1() { 6 System.out.println("cal1:"+cal1); 7 //非注入耦合 8 HttpServletRequest request = ServletActionContext.getRequest(); 9 request.setAttribute("cal1", cal1); 10 return "rs"; 11 }
测试: