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

相关文章

mybatis映射器配置小结

《mybatis映射器配置小结》本文详解MyBatis映射器配置,重点讲解字段映射的三种解决方案(别名、自动驼峰映射、resultMap),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录select中字段的映射问题使用SQL语句中的别名功能使用mapUnderscoreToCame

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令