eclipse学习(第二章:初识ssh)——5.struts2的action示例

我是看着这个网址学习做的一些内容记录,方便自己以后查找。学习的话可以自己去看

https://www.w3cschool.cn/struts_2/struts_actions.html

action的作用

Actions是Struts2框架的核心,因为它们适用于任何MVC(Model View Controller)框架。 每个URL映射到特定的action,其提供处理来自用户的请求所需的处理逻辑。
但action还有另外两个重要的功能。 首先,action在将数据从请求传递到视图(无论是JSP还是其他类型的结果)方面起着重要作用。 第二,action必须协助框架确定哪个结果应该呈现在响应请求的视图中。

action的创建

Struts2中actions的唯一要求是必须有一个无参数方法返回String或Result对象,并且必须是POJO。如果没有指定no-argument方法,则默认是使用execute()方法。
多数情况下我们会直接创建一个xxxAction,然后通过它去继承ActionSupport。那么为什么要继承ActionSupport呢?
struts2不要求我们自己设计的action类继承任何的struts基类或struts接口,但是我们为了方便实现我们自己的action,大多数情况下都会继承ActionSupport类,并重写此类里的public String execute() throws Exception方法。因为此类中实现了很多的实用借口,提供了很多默认方法,这些默认方法包括国际化信息的方法、默认的处理用户请求的方法等,这样可以大大的简化Acion的开发。
Struts2中通常直接使用Action来封装HTTP请求参数,因此,Action类里还应该包含与请求参数对应的属性,并且为属性提供对应的getter和setter方法。

而ActionSupport实际上又是实现了Action这个接口的

public interface Action {
   public static final String SUCCESS = "success";
   public static final String NONE = "none";
   public static final String ERROR = "error";
   public static final String INPUT = "input";
   public static final String LOGIN = "login";
   public String execute() throws Exception;
}

项目使用action的一个demo

1.创建一个动态web工程,引入相关的jar包到lib文件夹下。

2.创建多个action类,用于去实现action的逻辑判断。(我这里是通过对username属性的判断是否)

第一个PublicAction 是用来存放公有的属性,这里抽取出来了


import com.opensymphony.xwork2.ActionSupport;

public class PublicAction extends ActionSupport{
	public static String success = "success";
	
	public static String error = "error";
}

第二个HiAction 是用来做登录处理的


import com.opensymphony.xwork2.ActionSupport;

public class HiAction extends ActionSupport {
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public String execute() throws Exception {
		if("中文测试".equals(username)) {
			return PublicAction.success;
		}else {
			return PublicAction.error;
		}
	}

	private String username;
	
	
	
}

第三个主要作用是用来模拟多个action的情况


import com.opensymphony.xwork2.ActionSupport;

public class PlayAction extends ActionSupport {

	public String getPlayName() {
		return playName;
	}


	public void setPlayName(String playName) {
		this.playName = playName;
	}


	@Override
	public String execute() throws Exception {
		if("play".equals(playName)) {
			return PublicAction.success;
		}else {
			return PublicAction.error;
		}
	}

	private String playName;
	
}

3.web.xml文件内容如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ssh_learn_action</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <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>

4.在WEB-INF文件夹下面创建classes文件夹,然后在里面写一个struts.xml

创建struts.xml,这里为了模拟多action情况(可以在同一个package多action,或者多package多action),这里选择多package的方式
struts.xml

<?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>
	
	<include file="struts-1.xml"></include>
	<include file="struts-2.xml"></include>
	
	<!-- 下面的是在单个xml文件中用到的 -->
	<!--
	<constant name="struts.devMode" value="true"></constant>
	<package name="testHi" extends="struts-default">
		<action name="hi" class="com.learn.czx.HiAction" method="execute">
			<result name="success">hiWorld.jsp</result>
			<result name="error">errorWorld.jsp</result>
		</action>
	</package>
	
	<package name="testPlay" extends="struts-default">
		<action name="play" class="com.learn.czx.PlayAction" method="execute">
			<result name="success">PlayWorld.jsp</result>
			<result name="error">errorWorld.jsp</result>
		</action>
	</package>  
	-->
	
</struts>

struts-1.xml

<?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>
	<constant name="struts.devMode" value="true"></constant>
	
	<package name="testHi" extends="struts-default">
		<action name="hi" class="com.learn.czx.HiAction" method="execute">
			<result name="success">hiWorld.jsp</result>
			<result name="error">errorWorld.jsp</result>
		</action>
	</package>
	
	
</struts>

struts-2.xml

<?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>
	<constant name="struts.devMode" value="true"></constant>
	
	<package name="testPlay" extends="struts-default">
		<action name="play" class="com.learn.czx.PlayAction" method="execute">
			<result name="success">PlayWorld.jsp</result>
			<result name="error">errorWorld.jsp</result>
		</action>
	</package>
	
</struts>

5.创建4个页面。

index.jsp(登录主页),这里主要使用的是hi这个action

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
	<h1>欢迎来到登录页</h1>
	<form action="hi">
		<h1>请输入密码</h1>
		<input name="username"/><input type="submit" value="提交">
	</form>
</body>
</html>

hiWorld.jsp,登录成功会到这里去

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- 由于你导入了一些jar包,所以说你这里设定了s作为前缀来直接使用对应jar包内的方法 --%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hi World!</title>
</head>
<body>
	action成功打开的世界!
	<% /*
	struts2 框架中,在结果页面可通过struts2 的特有标签来获取后台属性,分别是:action中的属性、request中设置的属性、session中设置的属性。
	获取方式分别是:
	<s:property value="name"/><s:property value="#request.name"/><s:property value="#session.name"/>
	*/ %>
	<%-- 在这里是用来访问Action值栈中的普通属性的,这里是回调页面,内部方法处理完才到这的 --%>
	<s:property value="dsa"></s:property>
	
</body>
</html>

errorWorld.jsp,登录失败会到这里去

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>没有权限登录</title>
</head>
<body>
sorry,you can not login,you can check your countname or password
</body>
</html>

PlayWorld.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>第二个action测试</title>
</head>
<body>
	欢迎来到另一个action
</body>
</html>

6.测试

http://localhost:8080/ssh_learn_action/
在这里插入图片描述
在框内输入“中文测试”,不带双引号那种啊,然后点击提交,就可以跳转到这一页面
在这里插入图片描述
如果你输入的不是“中文测试”那么就会到这里去
在这里插入图片描述
接下来做另一个action测试,http://localhost:8080/ssh_learn_action/play?playName=play
在这里插入图片描述
如果playName的名字不为play则会去到
在这里插入图片描述

7.可能遇到的问题

https://blog.csdn.net/weixin_43987277/article/details/114750987

项目位置

这个文件夹里面的ssh_learn_action
https://gitee.com/mrchen13427566118/ssh_learn.git

如果不会怎么弄到eclipse的话,就看这里
https://blog.csdn.net/weixin_43987277/article/details/116936221

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值