Struts2基础入门

创建一个web工程

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <!-- i18n:国际化. 解决post提交乱码 -->
  7. <constant name="struts.i18n.encoding" value="UTF-8"></constant>
  8.  
  9. <!-- 指定访问action时的后缀名
  10. http://localhost:8080/struts2_day01/hello/HelloAction.action
  11. http://localhost:8080/struts2_day01/hello/HelloAction.do
  12. -->
  13. <constant name="struts.action.extension" value="action,do"></constant>
  14.  
  15. <!-- 指定struts2是否以开发模式运行
  16. 1.热加载主配置.(不需要重启即可生效)
  17. 2.提供更多错误信息输出,方便开发时的调试
  18. -->
  19. <constant name="struts.devMode" value="true"></constant>
  20.  
  21. <!-- package:将Action配置封装.就是可以在Package中配置很多action.
  22. name属性: 给包起个名字,起到标识作用.随便起.不能其他包名重复.
  23. namespace属性:给action的访问路径中定义一个命名空间
  24. extends属性: 继承一个 指定包
  25. abstract属性:包是否为抽象的; 标识性属性.标识该包不能独立运行.专门被继承
  26. -->
  27. <package name="hello" namespace="/hello" extends="struts-default" >
  28. <!-- action元素:配置action类
  29. name属性: 决定了Action访问资源名.
  30. class属性: action的完整类名
  31. method属性: 指定调用Action中的哪个方法来处理请求
  32. -->
  33. <action name="HelloAction" class="com.struts2.action.HelloAction" method="hello" >
  34. <!-- result元素:结果配置
  35. name属性: 标识结果处理的名称.与action方法的返回值对应.
  36. type属性: 指定调用哪一个result类来处理结果,默认使用转发.
  37. 标签体:填写页面的相对路径
  38. -->
  39. <result name="success" type="dispatcher" >/hello.jsp</result>
  40. </action>
  41. </package>
  42.  
  43. <!-- 引入其他struts配置文件 -->
  44. <include file="com/struts2/test/struts.xml"></include>
  45. <!--<include file="cn/itheima/c_default/struts.xml"></include>-->
  46. </struts>

src下的struts.xml

  1. package com.struts2.action;
  2.  
  3. /**
  4. * @author: 肖德子裕
  5. * @date: 2018/11/19 22:15
  6. * @description:
  7. */
  8. public class HelloAction {
  9. public String hello(){
  10. System.out.println("hello word");
  11. return "success";
  12. }
  13. }

HelloAction

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

  1. package com.struts2.test;
  2.  
  3. /**
  4. * @author: 肖德子裕
  5. * @date: 2018/11/20 8:58
  6. * @description:
  7. */
  8. public class DemoAction {
  9. public String add(){
  10. System.out.println("添加用户!");
  11. return "success";
  12. }
  13. public String delete(){
  14. System.out.println("删除用户!");
  15. return "success";
  16. }
  17. public String update(){
  18. System.out.println("修改用户!");
  19. return "success";
  20. }
  21. public String find(){
  22. System.out.println("查找用户!");
  23. return "success";
  24. }
  25. }

DemoAction

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <!-- 动态方法调用方式1:配置动态方法调用是否开启常量;默认是关闭的,需要开启;不建议使用 -->
  7. <!-- http://localhost:8080/demo/DemoAction!add.action -->
  8. <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
  9.  
  10. <package name="demo" namespace="/demo" extends="struts-default" >
  11. <!-- 动态方法调用方式2:通配符方式;使用{1}取出星号填充的内容;建议使用 -->
  12. <action name="DemoAction_*" class="com.struts2.test.DemoAction" method="{1}" >
  13. <result name="success" >/hello.jsp</result>
  14. </action>
  15. </package>
  16. </struts>

struts.xml

2)在web中配置拦截器

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6.  
  7. <!-- struts2核心过滤器 -->
  8. <filter>
  9. <filter-name>struts2</filter-name>
  10. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  11. </filter>
  12.  
  13. <filter-mapping>
  14. <filter-name>struts2</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>
  17. </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. (转)Linux命令:使用dig,nslookup命令解析域名

    Linux命令:使用dig命令解析域名 Linux下解析域名除了使用nslookup之外,开可以使用dig命令来解析域名,dig命令可以得到更多的域名信息. dig的全称是 (domain infor ...

  2. javascript基础语法备忘录-变量和数据类型

    //javascript基础语法备忘录-变量和数据类型 // 定义变量使用var关键字 后面跟变量名,不要使用eval 和arguments为变量名 var message = "hi&qu ...

  3. Oracle安装后忘记用户名或密码+创建新登陆用户

    新安装的Oracle11g,不料在使用的时候没记住安装时的用户名和密码. 不用担心,打开sqlplus. 按如下步骤,新建一个登陆用户: 第一步:以sys登陆  sys/密码 as sysdba  此 ...

  4. [转]完美的背景图全屏css代码 – background-size:cover?

    写主题样式的时候经常会碰到用背景图铺满整个背景的需求,这里分享下使用方法 需要的效果 图片以背景的形式铺满整个屏幕,不留空白区域 保持图像的纵横比(图片不变形) 图片居中 不出现滚动条 多浏览器支持 ...

  5. CentOS搭建KMS服务器

    安装 使用命令: #CentOS,Redhat,Fedora等请选择CentOS脚本 wget https://raw.githubusercontent.com/dakkidaze/one-key- ...

  6. 在基于WCF开发的Web Service导出WSDL定义问题及自定义wsdl:port 名称

             在契约优先的Web服务开发过程中,往往是先拿到WSDL服务定义,各家开发各自的服务实现或客户端,然后互相调用.          尽管Web Service的标准已经发布很多年,但各 ...

  7. Mysql远程连接授权IP

    新增法   我们现在增加一个'username'用户,密码为'password',让其能够从外部访问MYSQL. grant all on * to 'username' identified by ...

  8. 连接Mysql时出现java.math.BigInteger cannot be cast to java.lang.Long问题

    今天遇见这样一个坑.在连接数据库进行查询数据时,大家可能会遇见这样一个问题:java.math.BigInteger cannot be cast to java.lang.Long,然后去检查代码中 ...

  9. JAVA ------ 大牛

    李学凯 :http://blog.csdn.net/qq_27093465/article/details/51750535 码农场:http://www.hankcs.com/program/ 徐刘 ...

  10. Android 开发知识结构图