Struts2基础入门
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基础入门的更多相关文章
- Struts2入门1 Struts2基础知识
Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...
- SpringMVC基础入门
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
- SpringMVC基础入门,创建一个HelloWorld程序
ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...
- (转)Struts2快速入门
http://blog.csdn.net/yerenyuan_pku/article/details/66187307 Struts2框架的概述 Struts2是一种基于MVC模式的轻量级Web框架, ...
- 1、Struts2基本入门
一.了解了这几个主要的优点,会促使你考虑使用Struts2 : 1.POJO表单及POJO操作 - Struts2 去除掉了Struts框架中的Action Forms部分.在Struts2框架下,你 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- 「译」JUnit 5 系列:基础入门
原文地址:http://blog.codefx.org/libraries/junit-5-basics/ 原文日期:25, Feb, 2016 译文首发:Linesh 的博客:JUnit 5 系列: ...
- .NET正则表达式基础入门
这是我第一次写的博客,个人觉得十分不容易.以前看别人写的博客文字十分流畅,到自己来写却发现十分困难,还是感谢那些为技术而奉献自己力量的人吧. 本教程编写之前,博主阅读了<正则指引>这本入门 ...
- 从零3D基础入门XNA 4.0(2)——模型和BasicEffect
[题外话] 上一篇文章介绍了3D开发基础与XNA开发程序的整体结构,以及使用Model类的Draw方法将模型绘制到屏幕上.本文接着上一篇文章继续,介绍XNA中模型的结构.BasicEffect的使用以 ...
随机推荐
- (转)Linux命令:使用dig,nslookup命令解析域名
Linux命令:使用dig命令解析域名 Linux下解析域名除了使用nslookup之外,开可以使用dig命令来解析域名,dig命令可以得到更多的域名信息. dig的全称是 (domain infor ...
- javascript基础语法备忘录-变量和数据类型
//javascript基础语法备忘录-变量和数据类型 // 定义变量使用var关键字 后面跟变量名,不要使用eval 和arguments为变量名 var message = "hi&qu ...
- Oracle安装后忘记用户名或密码+创建新登陆用户
新安装的Oracle11g,不料在使用的时候没记住安装时的用户名和密码. 不用担心,打开sqlplus. 按如下步骤,新建一个登陆用户: 第一步:以sys登陆 sys/密码 as sysdba 此 ...
- [转]完美的背景图全屏css代码 – background-size:cover?
写主题样式的时候经常会碰到用背景图铺满整个背景的需求,这里分享下使用方法 需要的效果 图片以背景的形式铺满整个屏幕,不留空白区域 保持图像的纵横比(图片不变形) 图片居中 不出现滚动条 多浏览器支持 ...
- CentOS搭建KMS服务器
安装 使用命令: #CentOS,Redhat,Fedora等请选择CentOS脚本 wget https://raw.githubusercontent.com/dakkidaze/one-key- ...
- 在基于WCF开发的Web Service导出WSDL定义问题及自定义wsdl:port 名称
在契约优先的Web服务开发过程中,往往是先拿到WSDL服务定义,各家开发各自的服务实现或客户端,然后互相调用. 尽管Web Service的标准已经发布很多年,但各 ...
- Mysql远程连接授权IP
新增法 我们现在增加一个'username'用户,密码为'password',让其能够从外部访问MYSQL. grant all on * to 'username' identified by ...
- 连接Mysql时出现java.math.BigInteger cannot be cast to java.lang.Long问题
今天遇见这样一个坑.在连接数据库进行查询数据时,大家可能会遇见这样一个问题:java.math.BigInteger cannot be cast to java.lang.Long,然后去检查代码中 ...
- JAVA ------ 大牛
李学凯 :http://blog.csdn.net/qq_27093465/article/details/51750535 码农场:http://www.hankcs.com/program/ 徐刘 ...
- Android 开发知识结构图