版权声明:欢迎大家一起来交流学习!转载注明出处即可。 https://blog.csdn.net/Mliangydy/article/details/78436350
引用百度百科的一段话:“Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。”
我的理解是Struts2就是接收从前台传过来的数据并根据相应的请求跳转到另一个页面,具体的工作原理及其流程,我觉得这篇博客写的挺好的,清晰明了。从官网下载Struts2框架,解压,然后让我们一起动手搭建一个Struts2项目。
1.新建一个web项目,导入jar包
在项目的WEB-INF\lib目录下加入Struts2的jar包
- asm-5.2.jar
- asm-commons-5.2.jar
- asm-tree-5.2.jar
- commons-beanutils-1.9.2.jar
- commons-fileupload-1.3.3.jar
- commons-io-2.5.jar
- commons-lang-2.4.jar
- commons-lang3-3.6.jar
- commons-logging-1.1.3.jar
- freemarker-2.3.23.jar
- javassist-3.20.0-GA.jar
- log4j-api-2.8.2.jar
- ognl-3.1.15.jar
- struts2-core-2.5.13.jar
2.修改web.xml文件
在web.xml文件中配置Struts的核心过滤器:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>StrutsProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.创建业务控制器HelloAction类
创建action,要实现Action接口
package com.omg.action;
import com.opensymphony.xwork2.Action;
public class HelloAction2 implements Action {
@Override
public String execute() throws Exception {
return "SUCCESS";
}
}
4.配置struts.xml文件
- 这个文件要放在src目录下
- 官方文档没有给出这个文件,可以到官方给出的例子下找一个文件,直接复制到src目录下
- 这个文件的名称不能改,就叫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">
<!-- START SNIPPET: xworkSample -->
<struts>
<!-- Some or all of these can be flipped to true for debugging -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello" class="com.omg.action.HelloAction2">
<result name="SUCCESS">/HelloWorld.jsp</result>
</action>
</package>
</struts>
<!-- END SNIPPET: xworkSample -->
5.编写jsp页面
Hello.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>页面跳转</title>
</head>
<body>
<!-- hello是请求的action,也可以写成hello.action -->
<h3><a href="hello">从Hello.jsp页面跳转到HelloWorld.jsp</a></h3>
</body>
</html>
HelloWorld.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>Hello World</title>
</head>
<body>
<h2>欢迎来到Struts2世界</h2>
<h3>第一个程序,Hello world!</h3>
</body>
</html>
6.运行项目
将项目加入tomcat,然后运行,在浏览器地址栏输入路径(http://localhost:8080/StrutsConfigure/hello.jsp)
运行路径:
http://ip:port/项目名/action名称
action名称:对应struts.xml文件中action的名称
http://ip:port/项目名/action名称
action名称:对应struts.xml文件中action的名称
至此,一个简单的Struts2项目就搭建完成了。
总结:这次的配置还是相对比较简单的,没有出多大问题,主要是Struts2的流程关键词还不是很熟,需要多记和多背。