struts2_day01_入门案例

本文介绍如何使用Struts2框架搭建一个简单的Web应用,包括项目搭建、核心配置文件设置、页面编写、动作类实现及访问路径说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struts2开发包

入门案例所需jar包

 

入门案例步骤

创建项目,导入jar包

创建核心配置文件,添加约束

 在SRC下,创建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>

在web.xml中配置struts2核心过滤器

<?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>struts2_day01</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>

	<!-- 配置struts2核心过滤器 -->
	<filter>
		<filter-name>struts</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

编写页面

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>Insert title here</title>
</head>
<body>
	<h1>成功</h1>
</body>
</html>

success.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>
	<h1>Struts2的入门</h1>
	<a href="${ pageContext.request.contextPath }/aaa/helloStruts.action">Struts2的入门的访问路径</a>
</body>
</html>

编写核心配置文件

<?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:指定包的名称。必须写,必须唯一。
					extends:指定当前包的父包。子包自动具备父包的配置。我们的包一般都需要继承struts-default包
						该包中配置着struts2的核心,如果不继承该包,则不能使用struts2的核心
					abstract:把当前包声明为抽象包。抽象包就是用来被继承的。里面定义的公共配置。
						 只有没有action标签的包,才能声明为抽象包。
					namespace:指定当前包的名称空间。当包上指定了名称空间之后,访问URL就变成了:
							url = 名称空间+action标签的name取值
						它的作用就是把访问URL按照模块化来管理。
						名称空间的写法:
								必须以/开头
								后面的第一个字符必须是字母。
						体现模块化管理URL的例子:
							名称空间是:/user
							访问URL:
								/user/addUser.action   /user/updateUser.action.....
						当我们不指定名称空间时,它有默认值:
							默认值是:""
	-->
	<package name="user" namespace="/aaa" extends="struts-default" >

		<action name="helloStruts" class="com.itheima.web.HelloAction"
			method="sayHello">
			<result name="success">/success.jsp</result>
		</action>

	</package>

</struts>

编写动作类

package com.itheima.web;

/**
 * 用于处理请求的类
 * 
 * 在struts2中把用于处理请求类都称之为:动作类
 *
 */
public class HelloAction {
	
	/**
	 *  用于处理请求的方法
	 *  
	 *  把动作类中用于处理请求的方法都叫做:动作方法
	 *  
	 *  动作方法有编写规范:
	 *  	访问修饰符都是public
	 *  	返回值必须是String类型的
	 *  	方法不能有参数
	 * @return
	 */
	public String sayHello(){
		System.out.println("HelloAction中的sayHello方法执行了。。。。");
		return "success";
	}
 
}

访问路径

http://localhost:8080/struts2_day01/aaa/helloStruts.action

执行流程

 

 

注意:

红框内的只是在服务器启动的时候,执行一次

第7步:请求一次执行一次

action是多例模式,不用考虑线程安全问题

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值