Struts2(3)_Struts2 的第一个例子

本系列博客汇总在这里:Struts2 汇总


一、解压 struts2-blank.war

在这里插入图片描述

二、创建 WEB 工程

三、导入必要 jar 包

  • struts2-core-2.3.1.1.jar:Struts 2框架的核心类库。
  • xwork-core-2.3.1.1.jar:Command 模式框架,WebWork 和 Struts2 都基于 xwork
  • ognl-3.0.3.jar:对象图导航语言 (Object Graph Navigation Language),struts2 框架通过其读写对象的属性。
  • freemarker-2.3.18.jar:Struts 2 的 UI 标签的模板使用 FreeMarker 编写。
  • commons-logging-1.1.x.jar:ASF 出品的日志包,Struts 2 框架使用这个日志包来支持 Log4J 和 JDK 1.4+ 的日志记录。
  • commons-fileupload-1.2.2.jar:文件上传组件,2.1.6 版本后需要加入此文件。
  • commons-io-2.0.1.jar:传文件依赖的 jar 包。
  • commons-lang-2.5.jar:对 java.lang 包的增强。
  • javassist-3.11.0.GA.jar:是一个开源的分析、编辑和创建 Java 字节码的类库。

四、编写 JSP 页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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>
success
</body>
</html>

五、编写 Action 服务器端处理逻辑

Action 就是处理 request 请求的动作类,类似 servlet!

package com.wyx.action;
/**
 * action动作类
 */
public class HelloAction 
{	
	public String hello()
	{
		return "success";
	}
}

六、进行框架配置 web.xml、struts.xml

1、新建 struts.xml 文件

注意:新建 struts.xml 文件后,只有一些头信息,以下才是一个真正的空的 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>

2、配置 dtd 的提示信息

在这里插入图片描述
在这里插入图片描述
或者
在这里插入图片描述

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>
	<package name="hello" extends="struts-default">
		<action name="hello" class="com.wyx.action.HelloAction" method="hello">
			<result name="success">/success.jsp</result>
		</action>
	</package>
</struts>

4、编写 Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">	
 
  	<!-- 
  		服务器启动时该过滤器会被实例化
  	 -->
    <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>

七、运行测试

把项目部署到 tomcat以后,访问 http://localhost:8080/struts2_01/hello。
在这里插入图片描述
成功!

如有错误,欢迎指正!

发布了448 篇原创文章 · 获赞 210 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_36260974/article/details/103591589