Java Web:action配置,Action接口与ActionSupport基类,跳转,自定义【诗书画唱】
内容概括:action配置PPT,Action接口与ActionSupport基类,讲义 ,约定,tips,我朋友的视频友情推荐,个人对应的视频截图或文字笔记,个人的想法,个人观点,代码例子,跳转到注册页面的方法,跳转到登录页面的方法,继承的action的例子,提示”找不到需要运行的action代码“的功能实现。
个人的理解:继承ActionSupport后,就可以用不变名的execute方法外的自己自定义名的方法。


常常进行具体内容的内容概括会很方便找到期具体的内容
——诗书画唱
action配置PPT START









action配置PPT END
Action接口与ActionSupport基类PPT START





Action接口与ActionSupport基类PPT END
讲义 START

action的配置方式:
学习<action>标签的用法
方法1:创建一个java类,添加execute方法,然后进行配置,当method属性不写时,method的默认值就是execute
方法2:创建一个java类,添加任意名称的方法,通过method属性指定调用的方法就可以了
方法3:最简配置。单纯的页面跳转功能
action中的method属性如果是execute时,可以省略不写
action中的class属性如果是com.opensymphony.xwork2.ActionSupport时,可以省略不写
result中的name属性如果是success时,可以省略不写
以上的三个tips在框架中叫约定
讲义 END
我朋友的视频友情推荐 START
个人对应的视频截图或文字笔记:






个人的想法:其实关键就是有分模块的对应的配置。

以上2种方法可以同时存在!


action基本路径的构成 :

个人观点:一般来说我认为重要的内容,我就会记录下来,不然的话我就就是不会记录下来的。
我认为其实了解action的路径的构成其实还挺重要的。有时我还挺喜欢快速打字,敲键盘等的感觉的,因为感觉是很专注认真的时刻,很帅的感觉,其实你的外表,服装,发型等的外在和内在让别人认为你专注认真等等,就会尊敬且认为帅。






我朋友的视频友情推荐 END
代码例子 START
例子1 START
被框起来的要用的文件:



package com.jy.action;
public class NewYearAction {
//http://localhost:8080/J190802/ny/nyAc.action
public String execute(){
System.out.println("2021新年快乐");
return "success";
}
//http://localhost:8080/J190802/ny/hlAc.action
public String sayHello(){
System.out.println("Hello world");
return null;
}
}


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="my" namespace="/ny" extends="struts-default">
<action name="nyAc" class="com.jy.action.NewYearAction">
<result name="success">/index.jsp</result>
</action>
<action name="hlAc" class="com.jy.action.NewYearAction"
method="sayHello">
</action>
<!-- http://localhost:8888/J190802/ny/demoAc.action -->
<action name="demoAc" class="com.jy.action.DemoAction">
<result name="success">/index.jsp</result>
</action>
<action name="aaaAc" class="com.jy.action.DemoAction"
method="aaa">
<result name="input">/login.jsp</result>
</action>
</package>
<package name="pb" namespace="/pub" extends="struts-default">
<!-- 当找不到对应的action执行路径时,就会运行默认的action -->
<default-action-ref name="defaultAc"></default-action-ref>
<action name="toLoginAc" class="com.jy.action.PubAction"
method="toLogin">
<result name="login">/login.jsp</result>
</action>
<action name="toRegAc" class="com.jy.action.PubAction"
method="toReg">
<result name="reg">/reg.jsp</result>
</action>
<!-- http://localhost:8888/J190802/pub/welcomAc.action -->
<action name="welcomAc">
<result>/welcom.jsp</result>
</action>
<action name="defaultAc" class="com.jy.action.DefaultAction">
<result name="err">/error.jsp</result>
</action>
</package>
<package name="pro" namespace="/pro" extends="struts-default">
<action name="listAc" class="com.jy.action.ProductAction">
<result name="success">/product/list.jsp</result>
</action>
<action name="showAc" class="com.jy.action.ProductAction"
method="showMsg">
<result name="input">/product/msg.jsp</result>
</action>
<!-- http://localhost:8888/J190802/pro/testAc.action -->
<action name="testAc">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>



<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base hreff="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>2021新年快乐</h1>
</body>
</html>


<?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>J190802</display-name>
<!-- 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>
<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>
</web-app>


————
例子1 END
例子2 START
(一些配置的必要内容在例1中)



————

package com.jy.action;
import com.opensymphony.xwork2.Action;
public class ProductAction implements Action {
//http://localhost:8080/J190802/pro/listAc.action
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("展示所有商品");
return SUCCESS;
}
//http://localhost:8080/J190802/pro/showAc.action
public String showMsg(){
System.out.println("显示信息");
return INPUT;
}
}




——————

package com.jy.action;
public class DefaultAction {
public String execute(){
System.out.println("找不到需要运行的action代码");
return "err";
}
}


package com.jy.action;
import com.opensymphony.xwork2.ActionSupport;
public class DemoAction extends ActionSupport {
public String aaa(){
return INPUT;
}
}

————

package com.jy.action;
public class PubAction {
//跳转到登录页面的方法
//http://localhost:8080/J190802/pub/toLoginAc.action
public String toLogin(){
return "login";
}
//跳转到注册页面的方法
//http://localhost:8080/J190802/pub/toRegAc.action
public String toReg(){
return "reg";
}
}






例子2 END
继承的action的例子 START

package com.jy.action;
import com.opensymphony.xwork2.ActionSupport;
public class DemoAction extends ActionSupport {
public String aaa(){
return INPUT;
}
}




个人的理解:继承ActionSupport后,就可以用不变名的execute方法外的自己自定义名的方法。

继承的action的例子 END
提示”找不到需要运行的action代码“的功能实现 START

package com.jy.action;
//action就是一个普通的java类
public class HelloAction {
//方法签名不能变
//返回值必须是String
//方法名必须是exectue
//没有参数
//http://localhost:8080/J190802/pub/hlAc.action
public String execute(){
System.out.println("Hello world");
return "wel";
}
}


当xml配置文件中没对应的action代码时:

提示”找不到需要运行的action代码“的功能实现 END