struts2框架

如果你之前在MVC模式的时候一直都是通过servlet,获取和返回数据,那么现在开始学习struts2框架,

Struts是一个实现MVC设计模式的优秀的框架。它的许多优点我就不说了。

我用自己做的一张图说明servlet和struts2的区别。

写一个最基本的开发步骤,完成开发。

(1)创建WEB 工程

(2)导入必要jar包

(3) 编写JSP 页面

(4)编写Action 服务器端处理逻辑

(5)进行框架配置web.xml、struts.xml

(6)运行测试

(1)创建WEB 工程

         这步还是很正常一样,创建一个普通web工程。如图:

(2)导入必要jar包

      首先我要告诉你到哪里去下载这个框架:在apache官网,你百度struts2下载就可以找到了。

Struts运行必要jar包介绍:

开发中为了方便导入,可以使用app/struts2-blank.war 携带jar包

(3) 编写JSP 页面

在webRoot下新建一个Demo文件,网页先访问start.jsp之后通过框架返回到result页面

start.jsp界面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<h1>请求发起的页面</h1>
<!-- 发起一个请求 访问Struts2框架 -->
<!-- Struts2请求 默认是以.action结尾 -->
<a href="${pageContext.request.contextPath }/hello.action">访问Struts2入门程序</a>
</body>
</html>

result.jsp界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<h1>处理完成的结果页面</h1>
</body>
</html>

(4)编写Action 服务器端处理逻辑

这里就是通过java创建一个类,该类可以说是action最普通的一个类

 package com.yudian.struts;
public class HelloAction {
public String execute() throws Exception{ //这里取execute代表默认执行这个方法
System.out.println("我是执行的内容...");
return "excutesuccess"; //有返回值,我们用来跳转页面
}
}

(5)进行框架配置web.xml、struts.xml

配置文件才是核心关键,先配置web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<!-- 配置Struts2的前端控制器 --><!--filter-class里面很关键,固定 -->
<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> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

在配置struts.xml,注意了这个是放在src下面而不是包下面

 <?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="default" extends="struts-default">
<!-- 3.默认Action 和Action的默认处理类 -->
<!-- 1.初步认识Struts2 --><!-- 这里hello一定要和jsp中hello。action一致才能找到 -->
<action name="hello" class="com.yudian.struts.HelloAction">
<result name="excutesuccess">/demo/result.jsp</result>
</action> <!-- result代表如果返回值为 excutesuccess则跳转页面-->
</package>
</struts>

(6)运行测试

先通过浏览器访问start界面:

当点击:访问Struts2入门程序连接直接跳转到了result.jsp

运行结果:

这上面是一个最基本的struts框架的运用,很简单,主要是供大家理解,关于每一个细节我会在接下面慢慢补充.

欢迎大家的留言给出指点意见,谢谢!

struts2框架(1)---struts2入门的更多相关文章

  1. Struts2框架(8)---Struts2的输入校验

    Struts2的输入校验 在我们项目实际开发中在数据校验时,分为两种,一种是前端校验,一种是服务器校验: 客户端校验:主要是通过jsp写js脚本,它的优点很明显,就是输入错误的话提醒比较及时,能够减轻 ...

  2. Struts2框架之-Struts2的标签

    Struts2包含哪些标签? 解答: A: <s:a href=”"></s:a>—–超链接,类似于html里的<a></a> <s:a ...

  3. Struts2框架07 Struts2 + Spring + Mybatis 整合

    1 导包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  4. Struts2框架学习(一)

    Struts2框架学习(一) 1,Struts2框架介绍 Struts2框架是MVC流程框架,适合分层开发.框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计. 2 ...

  5. struts2框架

    详细教程 参考struts教程https://www.w3cschool.cn/struts_2/struts_configuration.html Struts2 基于MVC设计模式的web应用程序 ...

  6. 经典MVC框架技术-struts2基础知识

    Struts2框架简介 struts2框架是在struts1和webwork技术的基础上,进行合并的全新框架,struts2以Webwork为核心,采用拦截器来处理用户的请求,这样的设计使得业务逻辑控 ...

  7. Struts2笔记02——Struts2 概述(转)

    原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm Struts2是基于MVC设计模式的一种流行.成熟的We ...

  8. struts2框架快速入门小案例

    struts2快速入门: index.jsp------>HelloAction--------->hello.jsp struts2流程 1.导入jar包 struts2的目录结构: a ...

  9. Struts2框架入门

    1.1 Struts2概述: 是一个遵循WEB层规范的MVC设实现,该框架基本上借鉴了WebWork框架的体系结构,只吸收了少部分Struts1的优点.是目前JAVA EE项目中WEB层事实上的工业标 ...

随机推荐

  1. union: git command

    # switch one tag # warning: if do that, can't commit any change git clone $project_path git checkout ...

  2. ESP8266擦除工具完整安装

    ESP8266擦除工具完整安装 一.  ESP8266擦除工具路径:http://down.liangchan.net/ESP8266%B2%C1%B3%FD%B9%A4%BE%DF%CD%EA%D5 ...

  3. #实验三 敏捷开发与XP实践---实验报告

    一.实验三 敏捷开发与XP实践-1 1.实验要求 -实验三 敏捷开发与XP实践 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成I ...

  4. win10传奇手册CHM打开无法阅读解决

    今天在阅读传奇的帮助文档时候,突然遇到了一个问题.打开为空白. 如图所示  我这个情况打开的时候会提示 这个时候我们把 打开此文件总是询问 这个对勾 去掉 惊喜有没有. 哈哈 .有问题欢迎大家私信我!

  5. 利用IO和File类实现拷贝文件目录问题

    /* 复制文件夹 参数 File src,File dest */ public static void copy(File src,File dest){ if (src.isDirectory() ...

  6. oracle BLOG图片和CLOG base64码的转换

    --BASE64转图片CREATE OR REPLACE FUNCTION DECODE_BASE64(P_CLOB_IN IN CLOB) RETURN BLOB IS V_BLOB BLOB; V ...

  7. promise和生成器的结合

    if(Promise.wrap){ Promise.wrap = function(fn){ return function(){ var args = [].slice.call(arguments ...

  8. python中使用XPath

    XPath在Python的爬虫学习中,起着举足轻重的地位,对比正则表达式 re两者可以完成同样的工作,实现的功能也差不多,但XPath明显比re具有优势,在网页分析上使re退居二线. XPath介绍: ...

  9. DarwinStreamServer 6.0.3 rtsp服务器搭建

    14:46:34 环境:Centos 7.3 编译安装 1.下载Darwin源码 http://dss.macosforge.org/downloads/DarwinStreamingSrvr6.0. ...

  10. 利用clonezilla克隆、还原CentOS整个系统

    实现目的:全盘备份CentOS 6.0系统到U盘或者到移动硬盘 操作步骤: 1.制作再生龙镜像启动光盘或U盘,插入到要备份的CentOS 6.0 Linux上面,设置好开机启动(我这里用的是U盘,所以 ...