spring的struts简单介绍
之前一段时间学习了springmvc+mybatis+spring框架,突然对之前的struts东西有点陌生, 所以这里简单记录下温故而知新的东西吧。
1. 首先建立一个Dynamic Web Project, 下面是我建立的StrutsDemo项目目录,使用的是maven构建的, 自己又增加了几个文件夹

2. 给出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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>StrutsDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>StrutsDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.16</version>
</dependency> <!--下面这两个插件,是注解方式必须要加载的-->
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-convention-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.5.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-config-browser-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>2.5.16</version>
</dependency> </dependencies>
<build>
<finalName>StrutsDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
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>
<!-- 所有匹配*.action的请求都由struts2处理 -->
<constant name="struts.action.extension" value="action" />
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- struts配置文件改动后,是否重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 文件上传最大值 -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- Action名称中是否还是用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 允许标签中使用表达式语法 -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
<constant name="struts.dispatcher.parametersWorkaround" value="false" /> <package name="basePackage" extends="struts-default"> </package> </struts>
4. 给出web.xml文件:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name> <!-- add struts2 configiguration -->
<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>*.action</url-pattern>
</filter-mapping>
<!-- end add struts2 configuration--> <!-- 项目默认页 -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5. 给出TestAction.java文件:
package com.strutsdemo.action; import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result; @ParentPackage("basePackage")
@Action
@Namespace("/")
public class TestAction {
// http://localhost:8080/StrutsDemo/print!printSome.action
// 这里是通过注解的方式来输出前台页面,
// 大致的流程
// 1. 输入的url http://localhost:8080/StrutsDemo/print!printSome.action
// 2. action定位到TestAction的printSome方法, 因为注解value="print"
// 3. printSome方法返回print字符串, 而在我们注解results当中name="print",相互匹配, 所以会跳转到/page/print.jsp页面中
// 个人认为,通过注解方式可以很大程度上节省代码量,让代码更简单
@Action(value="print", results= {@Result(name="print", location="/page/print.jsp")})
public String printSome(){
System.out.println("TestAction printSome");
return "print";
} }
6. 给出print.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>
<h2>print something</h2>
</body>
</html>
7. 运行项目,并在浏览器中输入url: http://localhost:8080/StrutsDemo/print!printSome.action, 结果为:
print something
spring的struts简单介绍的更多相关文章
- spring cloud之简单介绍
以下是来自官方的一篇简单介绍: spring Cloud provides tools for developers to quickly build some of the common patte ...
- spring与struts简单整合案例
Spring,负责对象对象创建 Struts, 用Action处理请求 Spring与Struts框架整合, 关键点:让struts框架action对象的创建,交给spring完成! 步骤: 1)引入 ...
- Spring 工作流程简单介绍
Spring Web MVC 处理Http请求的大致过程: 一旦Http请求到来,DispatcherSevlet将负责将请求分发. DispatcherServlet可以认为是Spring提供的前端 ...
- Struts简单介绍
一.在介绍struts之前,先来了解一下什么是MVC框架吧. 1.MVC介绍 MVC全名是Model View Controller.是模型(model)-视图(view)-控制器(controlle ...
- Spring Cloud的简单介绍
参考地址: https://mp.weixin.qq.com/s/wG4CTLORnvemkjUsZ7YP6Q 从一个例子开始 对于这样的"大"问题,通常需要拆解成小问题来回答.要 ...
- Auto Layout深入理解,及masonry简单介绍
本篇博客是本人在学习自己主动布局过程中对自己主动布局的理解和整理,分三部分介绍,内容可能会有所反复.见谅. 一.autosizing与Auto Layout对照,及Auto Layout简单介绍 1. ...
- spring-boot-route(二十)Spring Task实现简单定时任务
Spring Task是Spring 3.0自带的定时任务,可以将它看作成一个轻量级的Quartz,功能虽然没有Quartz那样强大,但是使用起来非常简单,无需增加额外的依赖,可直接上手使用. 一 如 ...
- Spring Framework简单介绍
Spring Framework 学习java编程不知不觉已经三年时间了,開始的时候,总是喜欢看着视频,然后按部就班的敲打着键盘,每当系统正常执行后.心里乐开了花.最開始的时候,所有的代 ...
- Spring整合Struts的两种方式介绍
1 使用Spring托管Struts Action 该种方式就是将Struts Action也视为一种Bean交给Spring来进行托管,使用时Struts的配置文件中配置的Action的classs ...
随机推荐
- R语言最好的IDE——RStudio
转自http://www.dataguru.cn/article-1602-1.html 看到很多的R语言教材,介绍的编辑器或者IDE都是很简陋的那些,就没有见到有人提到RStudio.对于不使用Em ...
- 在命令行上启动genymotion虚拟机
自从有了genymotion,多机联调就解放了,一台电脑运行两个genymotion虚拟机毫无压力,不过也看用的是哪种os image,之前我以为google自己的Nexus应该最适应,哪知道开起来比 ...
- 转:基于InfluxDB&Grafana的JMeter实时性能测试数据的监控和展示
本文主要讲述如何利用JMeter监听器Backend Listener,配合使用InfluxDB+Grafana展示实时性能测试数据 关于JMeter实时测试数据 JMeter从2.11版本开始,命令 ...
- day1_2_3
DD烧写命令(mfgtools-without-rootfs.tar.gz) ubuntu minicom svn 应用层进程阻塞调试 多机共享 securecrt的远程登录以及调试 tengxunt ...
- 6.5 通过命令安装git
本来是一头雾水,以为很麻烦,结果,一句命令就搞定: 安装完后,在搜索框搜索,即出现github.
- “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第五篇(用户管理之“用户权限分配”)
一.在做权限分配之前,首先先了解“ZTree”这个插件,我的这个系统没有用Jquery-EasyUI的Tree.用的是”ZTree“朋友们可以试试,也很强大.点击下载ZTree插件. 1. ...
- vue中v-if 与v-show的区别
v-if vs v-show v-if 是“真正的”条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建. v-if 也是惰性的:如果在初始渲染时条件为假,则什么也不做—— ...
- iis应用程序池没有fromwork 4.0-----安装iis
找到已经安装的目录 C:\Windows\Microsoft.NET\Framework\v4.0.30319 以管理员身份运行一下就ok 安装iis 控制面板-程序与功能-打开与关闭window ...
- loj#6433. 「PKUSC2018」最大前缀和(状压dp)
传送门 今天\(PKUWC\)试机的题 看着边上的大佬们一个个\(A\)穿咱还是不会-- 我们考虑枚举最大前缀和,如果一个前缀\(1\)到\(p\)是最大前缀和,那么\(p\)后面的所有前缀和都要小于 ...
- HDU1272-小希迷宫
题目链接:点击打开链接 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首 ...