后端开发基础-Struts2框架学习-001—Struts2入门示例(HelloWorld)

Struts2入门示例(HelloWorld) 

#Struts2简介
 MVC框架,与SpringMVC,Struts1功能相同。
 Struts2与Struts1没有必然联系,它是从WebWork
框架发展而来,由Apache维护和推广。
#Struts2的体系结构(主要结构)
-StrutsPrepareAndExecuteFilter(主控制器)
  等价于SpringMVC的DispatcherServlet
-Action(业务控制器)
  等价于SpringMVC的Controller
-Result(视图响应组件)
 等价于SpringMVC的ViewResolver和jackson包

#Struts2的基本应用
请求-->Filter控制器-->Action业务控制器
-->Result响应组件-->hello.jsp-->响应输出
//Struts2默认请求类型为/hello.action或/hello
/hello.action
-->Filter控制器(识别.action或没有扩展名请求)
-->HelloAction(struts.xml)
-->Result响应组件(dispatcher)(struts.xml)
-->/WEB-INF/hello.jsp-->响应输出

http://localhost:8080/struts2-day01/hello.action
http://localhost:8080/struts2-day01/hello

核心组件: StrutsPrepareAndExecuteFilter

演示案例 

演示工程目录结构

需要导入Tomcat类库或者添加javaee.jar以便使用EL表达式 (jsp)

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.study</groupId>
  <artifactId>struts2-day01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
   <dependencies>
  	<dependency>
  		<groupId>org.apache.struts</groupId>
  		<artifactId>struts2-core</artifactId>
  		<version>2.1.8</version>
  	</dependency>
  </dependencies>
  
</project>

 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<!-- name唯一性标识
		extends继承框架里的Struts-default包 -->
	<package name="demo1" extends="struts-default">
		<!-- name请求名;class指定Action组件
			method指定Action方法名,默认execute -->
		<action name="hello" class="com.dk.action.HelloAction">
			<!-- 根据HelloAction.execute返回
				的String找到result定义 -->
			<result name="success" type="dispatcher">
				/WEB-INF/hello.jsp
			</result>
		</action>
	</package>
</struts>

HelloAction.java

package com.dk.action;

public class HelloAction {
	private String msg;//output

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	//默认请求处理方法名execute
	//public String xxx()
	public String execute(){
		msg = "这个是我的第一次Struts2示例";
		//调用Service+Dao逻辑 处理
		return "success";//对应result配置
	}

}

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=ISO-8859-1">
		<title>struts2-demo</title>
	</head>
	<body>
		<h2>Struts2入门示例(HelloWorld)</h2>
		<h3>${msg }</h3>
	</body>
</html>

加载工程struts2-day01,启动Tomcat运行程序,最终页面效果:

 

执行原理图示:

struts2配置文件默认存放路径在/WEB-INF/classes目录下,即将struts.xml放在src的目录下  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Boy_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值