Struts2基础入门

创建一个web工程

0)导包并且创建一个核心配置文件

<?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>
<!-- i18n:国际化. 解决post提交乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 指定访问action时的后缀名
http://localhost:8080/struts2_day01/hello/HelloAction.action
http://localhost:8080/struts2_day01/hello/HelloAction.do
-->
<constant name="struts.action.extension" value="action,do"></constant> <!-- 指定struts2是否以开发模式运行
1.热加载主配置.(不需要重启即可生效)
2.提供更多错误信息输出,方便开发时的调试
-->
<constant name="struts.devMode" value="true"></constant> <!-- package:将Action配置封装.就是可以在Package中配置很多action.
name属性: 给包起个名字,起到标识作用.随便起.不能其他包名重复.
namespace属性:给action的访问路径中定义一个命名空间
extends属性: 继承一个 指定包
abstract属性:包是否为抽象的; 标识性属性.标识该包不能独立运行.专门被继承
-->
<package name="hello" namespace="/hello" extends="struts-default" >
<!-- action元素:配置action类
name属性: 决定了Action访问资源名.
class属性: action的完整类名
method属性: 指定调用Action中的哪个方法来处理请求
-->
<action name="HelloAction" class="com.struts2.action.HelloAction" method="hello" >
<!-- result元素:结果配置
name属性: 标识结果处理的名称.与action方法的返回值对应.
type属性: 指定调用哪一个result类来处理结果,默认使用转发.
标签体:填写页面的相对路径
-->
<result name="success" type="dispatcher" >/hello.jsp</result>
</action>
</package> <!-- 引入其他struts配置文件 -->
<include file="com/struts2/test/struts.xml"></include>
<!--<include file="cn/itheima/c_default/struts.xml"></include>-->
</struts>

src下的struts.xml

package com.struts2.action;

/**
* @author: 肖德子裕
* @date: 2018/11/19 22:15
* @description:
*/
public class HelloAction {
public String hello(){
System.out.println("hello word");
return "success";
}
}

HelloAction

1)测试动态方法调用方式

package com.struts2.test;

/**
* @author: 肖德子裕
* @date: 2018/11/20 8:58
* @description:
*/
public class DemoAction {
public String add(){
System.out.println("添加用户!");
return "success";
}
public String delete(){
System.out.println("删除用户!");
return "success";
}
public String update(){
System.out.println("修改用户!");
return "success";
}
public String find(){
System.out.println("查找用户!");
return "success";
}
}

DemoAction

<?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>
<!-- 动态方法调用方式1:配置动态方法调用是否开启常量;默认是关闭的,需要开启;不建议使用 -->
<!-- http://localhost:8080/demo/DemoAction!add.action -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant> <package name="demo" namespace="/demo" extends="struts-default" >
<!-- 动态方法调用方式2:通配符方式;使用{1}取出星号填充的内容;建议使用 -->
<action name="DemoAction_*" class="com.struts2.test.DemoAction" method="{1}" >
<result name="success" >/hello.jsp</result>
</action>
</package>
</struts>

struts.xml

2)在web中配置拦截器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <!-- struts2核心过滤器 -->
<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>
</web-app>

3)测试访问hello word

http://localhost:8080/hello/HelloAction.action

4)测试访问增删改查

http://localhost:8080/demo/DemoAction_add.action

Struts2基础入门的更多相关文章

  1. Struts2入门1 Struts2基础知识

    Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...

  2. SpringMVC基础入门

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  3. SpringMVC基础入门,创建一个HelloWorld程序

    ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...

  4. (转)Struts2快速入门

    http://blog.csdn.net/yerenyuan_pku/article/details/66187307 Struts2框架的概述 Struts2是一种基于MVC模式的轻量级Web框架, ...

  5. 1、Struts2基本入门

    一.了解了这几个主要的优点,会促使你考虑使用Struts2 : 1.POJO表单及POJO操作 - Struts2 去除掉了Struts框架中的Action Forms部分.在Struts2框架下,你 ...

  6. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  7. 「译」JUnit 5 系列:基础入门

    原文地址:http://blog.codefx.org/libraries/junit-5-basics/ 原文日期:25, Feb, 2016 译文首发:Linesh 的博客:JUnit 5 系列: ...

  8. .NET正则表达式基础入门

    这是我第一次写的博客,个人觉得十分不容易.以前看别人写的博客文字十分流畅,到自己来写却发现十分困难,还是感谢那些为技术而奉献自己力量的人吧. 本教程编写之前,博主阅读了<正则指引>这本入门 ...

  9. 从零3D基础入门XNA 4.0(2)——模型和BasicEffect

    [题外话] 上一篇文章介绍了3D开发基础与XNA开发程序的整体结构,以及使用Model类的Draw方法将模型绘制到屏幕上.本文接着上一篇文章继续,介绍XNA中模型的结构.BasicEffect的使用以 ...

随机推荐

  1. get post put delete

    get:获取资源 幂等 post:执行不安全和非幂等操作(幂等:多次请求于一次请求的效果一样) put:更新资源  幂等 delete:删除资源  幂等 如何理解幂等: public book { p ...

  2. 五分钟急速搭建wordpress(博主亲测有效)

    第一步:下载WordPress安装包并解压 从此处下载WordPress压缩包并解压缩 http://wordpress.org/download/ 如果你想将WordPress上传至一个远程服务器, ...

  3. 一张图看懂offsetX, clientX, pageX, screenX的区别

    1.具体含义见下图1 2.浏览器的兼任情况

  4. css取消a标签在移动端点击时的背景颜色

    一.取消a标签在移动端点击时的蓝色 -webkit-tap-highlight-color: rgba(255, 255, 255, 0);-webkit-user-select: none;-moz ...

  5. 笨办法学Python(十六)

    习题 16: 读写文件 如果你做了上一个练习的加分习题,你应该已经了解了各种文件相关的命令(方法/函数).你应该记住的命令如下: close – 关闭文件.跟你编辑器的 文件->保存.. 一个意 ...

  6. Thymeleaf 随记

    一.基础写法: th:text='${数据}  ,其中text可以修改成其他,如href,value,class....看需求 <p th:text='${后台返回的数据}'>静态文本&l ...

  7. MySQL入门很简单: 10 mysql运算符

    1. 算术运算符 例子: 将t1表中字段a的值进行加法,减法和乘法 2. 比较运算符 注:LIKE经常和通配符"_"和"%"一起使用,"_" ...

  8. MySQL入门很简单: 5 索引

    1. 索引的含义和特点 索引:创建在表上,是对数据库表中一列或多列的值进行排序的一种结构. 存储类型: B性树(BTREE)索引和哈希(HASH)索引: InnoDB和MyISAM支持BTREE索引, ...

  9. 1929. Teddybears are not for Everyone (Timus) (combination+reading questions)

    http://acm.timus.ru/problem.aspx?space=1&num=1929 combination problems. 排列组合问题. According to the ...

  10. selenium安装及官方文档

    selenium-python官方文档: https://selenium-python.readthedocs.io/ python3.5已安装的情况下,安装示意图如下 命令行输入 pip3 ins ...