struts介绍
目录
Struts环境搭建
动态方法调用
jsp传递参数到后台
后台传递到jsp
struts环境搭建
struts的配置是在maven的环境全部配置好的前提下,在maven项目中的pom.xml加入我们struts的依赖,加载jar包
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
然后再导入我们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>
<include file="struts-default.xml"></include>
<include file="struts-base.xml"></include>
<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>
<!-- /user/bookAction?methodName=list
/sy/bookAction?methodName=list
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/demo_*" class="com.wangshaoyang.web.HelloAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
</package>
</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" />
<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>
struts动态调用方法
先写好的我们的控制器,HelloAction ,在里面写两个测试方法
public class DemoAction implements ModelDriven<Cal>,ServletRequestAware {
public String add() {
System.out.println("调用add方法");
return "rs";
}
public String del() {
System.out.println("调用del方法");
return "rs";
}
}
写一个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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>动态方法调用</h3>
<a href="${pageContext.request.contextPath }/sy/demo_add.action">新增</a>
<a href="${pageContext.request.contextPath }/sy/demo_del.action">删除</a>
</body>
</html>
结果每次点击控制台都打印了相对应的方法,所有说是成功了
struts前台和后台参数的传递
jsp参数传递到后台有三种方式
```
1、implements modelDrivern(通过实现接口来接收数据)
2、set/get(直接通过实体类的方法获取)
3、类实例.属性名(直接写类的属性名)
```
我们首先准备一个实体类Cal,好存放我们的参数
package com.wangshaoyang.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 + "]";
}
}
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>后台接收jsp传递参数的三种方式</h3>
<a href="${pageContext.request.contextPath }/sy/demo_accept1.action?num1=20&&num2=5">accept1</a>
<a href="${pageContext.request.contextPath }/sy/demo_accept2.action?cal2.num1=20&&cal2.num2=5">accept2</a>
<a href="${pageContext.request.contextPath }/sy/demo_accept3.action?sex=nv">accept3</a>
</body>
</html>
后台传值到jsp界面
一是注入的,直接实现ServletRequestAware接口,通过req存放
二是非注入的,在方法中通过ServletActionContext.getRequest(),实例req来存放
package com.wangshaoyang.web;
import java.util.Map;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wangshaoyang.entity.Cal;
public class DemoAction implements ModelDriven<Cal>,ServletRequestAware {
// 注入耦合
private HttpServletRequest req;
private Cal cal1=new Cal();
private Cal cal2;
private String sex;
public Cal getCal2() {
return cal2;
}
public void setCal2(Cal cal2) {
this.cal2 = cal2;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
/**
* implements ModelDriven
* @return
*/
public String accept1() {
System.out.println("cal1:"+cal1);
// req.setAttribute("cal1",cal1);
// 非注入耦合
HttpServletRequest req=ServletActionContext.getRequest();
req.setAttribute("cal1", cal1);
// 非注入解耦
// ActionContex context=ActionContext.getContext();
// context.get("xxxxxxx");
return "rs";
}
/**
* 类实例.属性名 接受参数值
* @return
*/
public String accept2() {
System.out.println("cal2:"+cal2);
return "rs";
}
/**
* set/get 接受参赛者
* @return
*/
public String accept3() {
System.out.println(sex);
return "rs";
}
public String add() {
System.out.println("调用add方法.......");
return "rs";
}
public String del() {
System.out.println("调用del方法.......");
return "rs";
}
@Override
public Cal getModel() {
return cal1;
}
@Override
public void setServletRequest(HttpServletRequest req) {
this.req=req;
req.setAttribute("cal1", cal1);
}
}
最后在jsp页面测试,rs.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>
结果页:${sex }
<br/>
${cal1 }
${cal2 }
</body>
</html>
我们就可以得到
end…