7.10--SSH学习之Struts2 Action配置

2024-02-09 15:18

本文主要是介绍7.10--SSH学习之Struts2 Action配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在说struts2之前当然要先下载好其框架了,官网有推荐
压缩包含:
apps:使用struts开发的一些demo
src:一个示例
docs:文档
lib:jar包


三种创建Action的方式

  1. 创建普通类,编写execute()方法
  2. 创建Action类,实现Action接口
  3. 创建Action类,继承ActionSupport类

    示例一:
public class FirstAction {public String execute()throws Exception{System.out.println("in FirstAction method execute()");return "success";}}


示例二:

public class SecondAction implements Action {@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("in SecondAction method execute()");return SUCCESS;}}


示例三:

public class ThirdAction extends ActionSupport {@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("in ThirdAction method execute()");return "success";}}

三种调用Action方法的方式

  1. 调用execute()方法响应客户端请求
  2. 动态方法调用
  3. 调用指定名字的方法响应客户端请求, 一个Action 类可包含多个方法,最好是3-5个

    struts.xml
<!-- 动态方法盗用 --><constant name="struts.enable.DynamicMethodInvocation" value="true" /><constant name="struts.devMode" value="true" /><package name="default" namespace="/" extends="struts-default"><!-- 第一种调用方式,execute()方法,如后面有method="",则不调用默认execute().而调用method指定的方法              --><action name="first" class="com.su.web.action.FirstAction"><result type="dispatcher" name="success">/success.jsp</result></action><action name="stu" class="com.su.web.action.StudentAction"><result type="dispatcher" name="success">/success.jsp</result></action><!-- 第二种调用方式,动态调用      stu!addStudent,调用StudentAction类中的addStudent方法--><action name="addStu" class="com.su.web.action.StudentAction" method="addStudent"><result type="dispatcher" name="success">/success.jsp</result></action><action name="updateStu" class="com.su.web.action.StudentAction" method="updateStudent"><result type="dispatcher" name="success">/success.jsp</result></action><action name="deleteStu" class="com.su.web.action.StudentAction" method="deleteStudent"><result type="dispatcher" name="success">/success.jsp</result></action><!-- 第三种调用方式,调用指定的方法 ,stu_addStudent,后面的{1}中为addStudent,也称为占位符调用--><action name="stu_*" class="com.su.web.action.StudentAction" method="{1}"><result type="dispatcher" name="success">/success_{1}.jsp</result></action><!-- 后缀有两个时,可往后加<action name="stu_*_*" class="com.su.web.action.StudentAction" method="{1}_{2}"><result type="dispatcher" name="success">/success_{1}_{2}.jsp</result></action>-->


示例一:

 <form action="first" method="post"><input type="submit" value="提交"></form>


示例二:

<form action="stu!addStudent" method="post"><input type="submit" value="提交"></form>


示例三:

form action="stu_addStudent" method="post"><input type="submit" value="提交"></form>

三种接收表单数据的方式

  1. Action的普通属性传参
  2. Action对象属性传参
  3. ModekDriven传参(缺点是只能由一个实体)

示例一:

public class UserAction extends ActionSupport {//普通属性传参private String userName;private String userPwd;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserPwd() {return userPwd;}public void setUserPwd(String userPwd) {this.userPwd = userPwd;}public String execute() throws Exception{System.out.println("in execute!!!");System.out.println(userName);System.out.println(userPwd);return "success";}}


示例二:

public class UserTwoAction extends ActionSupport{//对象属性传参private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String execute() throws Exception{System.out.println("in execute!!!");System.out.println(user.getUserName());System.out.println(user.getUserPwd());ActionContext.getContext().put("message", "!!!密码或用户名错误!!!");return "success";}public String userlogin() throws Exception{System.out.println("in userlogin!!!");System.out.println(user.getUserName());System.out.println(user.getUserPwd());if(user.getUserName().equals("susu") && user.getUserPwd().equals("1111")){ActionContext.getContext().put("message", "!!!登录成功!!!");return "success";}return "nextAction";}
}


示例三:

    //ModelDriven传参private User user= new User();public User getUser() {return user;}public void setUser(User user) {this.user = user;}//@Overridepublic User getModel() {// TODO Auto-generated method stubreturn user;}public String userlogin() throws Exception{System.out.println("in userlogin!!!");System.out.println(user.getUserName());System.out.println(user.getUserPwd());return "success";}}


表单JSP页面

  <body><form action="user_userlogin" method="post"><!-- 普通属性传参 --><!--  用户名:<input type="text" name="userName">密&nbsp;&nbsp;码:<input type="password" name="userPwd"><input type="submit" value="登录">--><!-- 对象属性传参,表单元素的名字就是:Action的属性名.Action属性的属性名 --><!--  用户名:<input type="text" name="user.userName">密&nbsp;&nbsp;码:<input type="password" name="user.userPwd"><input type="submit" value="登录">-->用户名:<input type="text" name="userName">密&nbsp;&nbsp;码:<input type="password" name="userPwd"><input type="submit" value="登录"></form></body>

四种Action请求下一个资源的方法

  1. dispatcher转发,Action To JSP
  2. chain转发,Action To Action
  3. redirect重定向,Action To JSP
  4. redirectAction 重定向,Action To Action
PS:转发:在原JSP页面提交的用户名和密码之类的信息不丢失
    重定向:则丢失


示例一:

        <action name="userTwo" class="com.su.web.action.UserTwoAction" ><result type="dispatcher" name="success">/indexRedict.jsp</result></action>


示例二:

        <action name="userTwo_*" class="com.su.web.action.UserTwoAction" method="{1}"><result type="chain" name="nextAction">userTwo</result><result type="dispatcher" name="success">/success_user.jsp</result></action>


示例三:

        <action name="userTwo_*" class="com.su.web.action.UserTwoAction" method="{1}"><result type="redirectAction" name="nextAction">userTwo</result><result type="redirect" name="success">/success_user.jsp</result></action>

Author:su1573

这篇关于7.10--SSH学习之Struts2 Action配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/694562

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

Zookeeper安装和配置说明

一、Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。 ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境; ■ 伪集群模式:就是在一台物理机上运行多个Zookeeper 实例; ■ 集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble) Zookeeper通过复制来实现

CentOS7安装配置mysql5.7 tar免安装版

一、CentOS7.4系统自带mariadb # 查看系统自带的Mariadb[root@localhost~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64# 卸载系统自带的Mariadb[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7

hadoop开启回收站配置

开启回收站功能,可以将删除的文件在不超时的情况下,恢复原数据,起到防止误删除、备份等作用。 开启回收站功能参数说明 (1)默认值fs.trash.interval = 0,0表示禁用回收站;其他值表示设置文件的存活时间。 (2)默认值fs.trash.checkpoint.interval = 0,检查回收站的间隔时间。如果该值为0,则该值设置和fs.trash.interval的参数值相等。

NameNode内存生产配置

Hadoop2.x 系列,配置 NameNode 内存 NameNode 内存默认 2000m ,如果服务器内存 4G , NameNode 内存可以配置 3g 。在 hadoop-env.sh 文件中配置如下。 HADOOP_NAMENODE_OPTS=-Xmx3072m Hadoop3.x 系列,配置 Nam

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

wolfSSL参数设置或配置项解释

1. wolfCrypt Only 解释:wolfCrypt是一个开源的、轻量级的、可移植的加密库,支持多种加密算法和协议。选择“wolfCrypt Only”意味着系统或应用将仅使用wolfCrypt库进行加密操作,而不依赖其他加密库。 2. DTLS Support 解释:DTLS(Datagram Transport Layer Security)是一种基于UDP的安全协议,提供类似于

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal