Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】
1 什么是Struts2框架
基于MVC设计模式的web应用框架
Struts2框架是一个轻量级的MVC流程框架
轻量级是指程序的代码不是很多,运行时占用的资源不是很多,MVC流程框架就是说它是支持分层开发,控制数据的流程,从哪里来,到那里去,怎么来,怎么去的这样一个框架;
Struts1 和 Struts2 没有任何关系
Struts2的前身是WebWork
Struts1/Struts2/SpringMVC都是MVC设计模式的表现层框架【SpringMVC现在最流行】
2 如何使用Struts2框架
2.1 导包 Struts2-core
2.2 配置主控制器,在web.xml中进行配置(注意:这里的主控制器相当于SpringMVC的前端控制器)
2.3 配置struts.xml配置文件(注意:这里的配置文件名不能进行更改)
3 案例:使用Struts2表现框架实现一个helloworld案例
3.1 案例效果
在浏览器输入:http://localhost:8080/ssh01/demo/hello
服务器返回的结果是:

3.2 配置主控制器

注意:主控制器类在maven中的位置

3.3 配置struts.xml配置文件
3.3.1 手动创建一个struts.xml文件
》创建一个xml文件,文件名为struts
》打开默认的struts.xml文件

》将默认struts.xml文件中24——26中内容复制到自己创建的struts.xml文件中

》在自己创建的struts.xml文件中添加一个<struts></struts>标签
3.3.2 配置struts.xml文件
》配置请求路径
通过 package 标签实现
name属性:随便写个名字就行,无什么实际意义
namespace属性:请求路径
extends属性:继承默认的struts.xml配置文件
》配置具体请求名
通过 action 标签实现
name属性:具体的请求名(注意:会默认在后面添加 .action ,所以访问时使用 hello 和 hello.action 都可以)
class属性:请求控制类
》配置返回的结果
通过 result 标签实现
name属性:该属性值必须和控制类中execute方法的返回值保持一致

3.4 编写控制类
该类中必须有一个execute方法,该方法是用来处理请求的

<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>cn.xiangxu</groupId>
<artifactId>ssh01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
</dependency>
</dependencies>
</project>
pom.xml (maven依赖)
<?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" version="2.5">
<display-name>ssh01</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> <!-- 配置主控制器
配置方法和selvet的配置相似
相当于SpringMVC框架的前端控制器 -->
<filter>
<filter-name>StrutsMVC</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsMVC</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
web.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="test" namespace="/demo" extends="struts-default">
<action name="hello" class="cn.xiangxu.action.HelloAction">
<result name="success">
/WEB-INF/msg.jsp
</result>
</action>
</package> </struts>
struts.xml (struts框架配置文件)
package cn.xiangxu.action;
public class HelloAction {
public String execute() {
System.out.println("hello struts2");
return "success";
}
}
控制类
<%@ 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>
<h2>Hello Struts2</h2>
</body>
</html>
msg.jsp (结果页面)

Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】的更多相关文章
- ruby利用Zip Gem写一个简单的压缩和解压的小工具
在UNIX下的我们怎么会沦落到用ruby写压缩和解压工具呢?直接上shell啊!但是请允许本猫这次可耻的用ruby来玩玩吧!其实ruby GEM中有很多压缩解压包,我选的是Zip,也许是因为名字符合K ...
- python利用scapy模块写一个TCP路由追踪和扫描存活IP的脚本
前言: 没有前言 0x01 from scapy.all import * import sys from socket import * import os from threading impor ...
- 利用伪类写一个自定义checkbox和radio
首先是效果图来一张 再来一张html结构 关键的CSS来了~ 首先呢要把input标签设置为display: none; 因为自定义的原理是通过label的for属性,来点击label转向为点击in ...
- 【阿菜做实践】利用go语言写一个简单的Pow样例
本篇博客的主要内容是用go写一个简单的Proof-of-Work共识机制,不涉及到网络通信环节,只是一个本地的简单demo.开发IDE用的是JB Golang. 整个项目的文件结构如下: PoWdem ...
- UEC 利用代理/委托写一个生命组件
首先基于ActorComponent创建一个组件 HealthComponent,将需要的变量与函数创建 #include "CoreMinimal.h" #include &qu ...
- 通过Struts了解MVC框架,兼说如何在面试中利用Struts证明自己
虽然目前Struts MVC框架不怎么用了,但它确是个能帮助大家很好地入门Web MVC框架,而且,一些历史项目可能还用Struts,反正技多不压身,大家如果能在面试中通过项目证明自己Struts这块 ...
- Struts2开山篇【引入Struts、自定义MyStruts框架】
前言 这是Strtus的开山篇,主要是引入struts框架-为什么要引入struts,引入struts的好处是什么-. 为什么要引入struts? 首先,在讲解struts之前,我们来看看我们以前写的 ...
- Spring集成Struts、Hibernate----三大框架SSH(Spring、Struts和hibernate)
Spring 框架可以完成业务层的设计任务,Struts框架可以将表示层和业务层分离,而Hibernate框架可以提供灵活的持久层支持.下面介绍三大框架的集成环境: 1.配置Struts2. I.导入 ...
- struts2.3.20+spring4.0.2+hibernate4.3.4框架整合
一.创建web工程,搭建Struts框架开发环境: 这里只导入了项目中所需要的重要的jar包,以后根据业务要求继续导入相关的包. 步骤1::导入struts框架所需的jar包 步骤2:在web.xml ...
随机推荐
- MySQL分片 --转自Peter Zaitsev对MySQL分片的建议
本文作者Peter Zaitsev是知名数据库专家,2006年联合创立了Percona.负责维护网站“MySQL性能”.同时,他也是<高性能MySQL>一书的联合作者.以下是他对MySQL ...
- AOP注解式拦截
1. 自己定义的拦截注解 package com.spring.aop; import java.lang.annotation.Documented; import java.lang.annota ...
- 进程、线程、ThreadLlocal
1.线程是最小的执行单位,而进程中至少一个线程组:如果调度进程和线程,完全由操作系统决定,程序自己不能决定什么时候执行,执行多长时间 Unix/Linux操作系统提供了一个fork()系统调用,它非常 ...
- python数据类型、操作符
python中数据类型包含:int,float,boolean,string,list(列表),set(集合),dictionary(字典) 数据类型转换: ①字符串 转 int:>>&g ...
- C#面向对象(四):其他面向对象知识
前文链接: C#面向对象(一):明确几个简单的概念作为开胃菜 C#面向对象(二):封装和继承 C#面向对象(三):多态 今天是这个系列的收尾文章了,来谈谈其他面向对象知识. 1.嵌套类 1.1概念 在 ...
- CommonJS 规范
CommonJS 是以在浏览器环境之外构建 JavaScript 生态系统为目标而产生的项目,比如在服务器和桌面环境中. 这个项目最开始是由 Mozilla 的工程师 Kevin Dangoor 在2 ...
- FastAdmin 的 Bootstrap-Table 如何合并字段?
FastAdmin 的 Bootstrap-Table 如何合并字段? ★hey-成都 14:13:34 把下面那个字段合并到上面那个字段是用什么方法 ^★暗物质-江西 14:17:21 city加上 ...
- 玩转C链表
链表是C语言编程中常用的数据结构,比如我们要建一个整数链表,一般可能这么定义: 1 2 3 4 struct int_node { int val; struct in ...
- Jeesite开垦
1. userIndex里面,$ctx是在哪里定义的? 就是request.context 2. 增加新的包后,扫描配置修改1) spring-context.xml文件中,扫描非@controlle ...
- 洛谷P4721 【模板】分治 FFT(分治FFT)
传送门 多项式求逆的解法看这里 我们考虑用分治 假设现在已经求出了$[l,mid]$的答案,要计算他们对$[mid+1,r]$的答案的影响 那么对右边部分的点$f_x$的影响就是$f_x+=\sum_ ...