1.Struts2简介
Struts2是Struts1的下一代产品,是在 struts1和WebWork的技术基础上进行了合并的全新的Struts 2框架。 其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。 虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。
Struts2是一个基于MVC设计模式的Web层框架。Web层的框架都采用前端控制器模式。
2.环境搭建
2.1 需要导入的jar包
2.2添加过滤器
修改web.xml文件
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2Test1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
必须配置Struts2的前端控制器,注意:这一步是必须要做的操作,这是Struts2核心的控制器
2.3 struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- namespace为名称空间,一般与<action>标签中的name属性共同决定访问路径。extends表示继承的包,一般都是继承struts-default -->
<package name="default" namespace="/" extends="struts-default">
<!-- name表示jsp访问的路径,如http://localhost:8080/struts2Test1/hello.action-->
<!--class配置Action类的全路径,method:Action类中执行的方法,如果不指定,默认值是execute(前提是ACtion类继承ActionSupport) -->
<action name="hello" class="com.mq.web.HelloAction" method="firstAction">
<!-- 如果匹配的Action方法返回hello,那么跳转到hello.jsp页面 -->
<result name="hello">/hello.jsp</result>
</action>
</package>
</struts>
struts.xml配置文件放置在src目录下,并且名称不要随便改变。
2.4 编写Action类
public class HelloAction {
public String firstAction() {
System.out.println("hello struts2!!");
return "hello";
}
}
3. Struts2的执行流程
JSP页面–>StrutsPrepereAndExecuteFilter过滤器–>执行一系列拦截器(完成了部分代码)–>执行到目标Action–>返回字符串–>结果页面(result)–>页面跳转
4.Action类的编写和Action的访问
Action类:
public class DemoAction extends ActionSupport {
public String firstAction() {
System.out.println("hello struts2!!");
return "hello";
}
//这是ActionSupport的方法,重写此方法时,如果Action配置标签中没有指明method,那么就执行execute方法。
@Override
public String execute() {
System.out.println("execute...");
//返回NONE,表示页面不转向
return NONE;
}
public String demo1() {
System.out.println("收到请求了");
return SUCCESS;
}
public String demo2() {
System.out.println("通配符的方式");
return SUCCESS;
}
public String saveUser(){
System.out.println("保存用户");
return SUCCESS;
}
public String deleteUser(){
System.out.println("删除用户");
return "delete";
}
}
struts.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 包结构,必须继承 struts-default-->
<package name="demo1" namespace="/" extends="struts-default">
<!-- 配置Action -->
<action name="hello" class="com.mq.web.DemoAction" method="firstAction">
<!--firstAction的返回值为hello的时候,则跳转到hello.jsp -->
<result name="hello">/hello.jsp</result>
</action>
<action name="demo" class="com.mq.web.DemoAction" method="demo1">
<!--firstAction的返回值为success的时候,则跳转到hello.jsp -->
<result name="success">/hello.jsp</result>
</action>
<!--不写method标签时,默认执行ActionSupport中的execute方法。 -->
<action name="demo2" class="com.mq.web.DemoAction">
</action>
<!--使用通配符,如果访问的路径是user_saveUser.action,那么*代表saveUser,method的值是saveUser,
该请求执行的方法是DemoAction类中的saveUser方法 -->
<action name="user_*" class="com.mq.web.DemoAction" method="{1}">
<!--firstAction的返回值为success的时候,则跳转到hello.jsp -->
<result name="success">/hello.jsp</result>
<result name="delete">/deleteUser.jsp</result>
</action>
</package>
</struts>
页面:
<%@ 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>
<a href="${pageContext.request.contextPath}/hello.action">hello struts2</a><br>
<a href="${pageContext.request.contextPath}/demo.action">demo struts2</a><br>
<a href="${pageContext.request.contextPath}/demo2.action">测试ActionSupport的默认方法</a><br>
<a href="${pageContext.request.contextPath}/user_saveUser.action">保存用户,action中使用了通配符</a><br>
<a href="${pageContext.request.contextPath}/user_deleteUser.action">删除用户,action中使用了通配符</a><br>
</body>
</html>
ActionSupport提供的方法和常量:
* Action接口中定义了5个常量,5个常量的值对应的是5个逻辑视图跳转页面(跳转的页面还是需要自己来配置),还定义了一个方法,execute方法。
* 5个常量
* SUCCESS -- 成功.
* INPUT -- 用于数据表单校验.如果校验失败,跳转INPUT视图.
* LOGIN -- 登录.
* ERROR -- 错误.
* NONE -- 页面不转向.