MOMOGONG 2022-09-15 11:08 采纳率: 57.1%
浏览 23
已结题

IDEA Struts2 There is no Action

  1. 问题背景:在IDEA使用Struts2,运行后获取不到传入的值或者键值信息没有传入到后台,以及There is no Action mapped for action name propertyAction.

  2. 用代码块功能插入代码

- 在com/in/action/api/GetServletAPIAction1.java

package com.in.action.api;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Map;

public class GetServletAPIAction1 extends ActionSupport {
    public GetServletAPIAction1() {
    }

    @Override
    public String toString() {
        return "GetServletAPIAction1{}";
    }


    //1与Servlet解耦合的非IOC方式
    public String execute() throws Exception{

        //1.1获取Action的上下文
        ActionContext context=ActionContext.getContext();
        //1.2相当于获取javax.servlet.http.HttpServletRequest对象
        //Map req=context.getPramters();
        //1.3相当于获取application
        Map as=context.getApplication();
        //1.4相当于获取javax.servlet.http.HttpSession
        Map ses=context.getSession();

        context.put("req","从request范围获取");
        as.put("appl","从application范围获取");
        ses.put("sess","从session范围获取");


        return "success";
    }
}


  • 在com/in/action/api/biaoqian/PropertyAction.java
package com.in.action.api.biaoqian;

import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class PropertyAction {
     public String execute(){
        HttpServletRequest request=ServletActionContext.getRequest();
        HttpSession session=request.getSession();
        ServletContext application=ServletActionContext.getServletContext();

        request.setAttribute("req","");
        session.setAttribute("sess","session");
        application.setAttribute("app","appli范围");

        return "success";
     }
}

  • 在web/pages/api/servlet_api_1.jsp中
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: 戴尔
  Date: 2022/9/13
  Time: 13:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
    <head>
        <title>servlet_api_1</title>
    </head>
    <body>
        <s:debug></s:debug>
        <%
            System.out.println("可以吗?");

        %>
        <%=request.getParameter("req")%><br>
      request:${requestScope.req}<br>
      application:${applicationScope.appl}<br>
      session:${sessionScope.sess}<br>

        <h4>以下使用scope.getAttribute的形式来接受</h4>

        request:      <%=request.getAttribute("req") %><br>

        session:       <%=session.getAttribute("sess") %><br>

        application:<%=application.getAttribute("appl") %><br>
    </body>
</html>



  • 在web/pages/biaoqian/property.jsp中
<%--
  Created by IntelliJ IDEA.
  User: 戴尔
  Date: 2022/9/15
  Time: 8:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        EL表达式:<br>
        request:${requestScope.req}<br>
        session:${sessionScope.sess}<br>
        application:${applicationScope.app}<br>
        valuestack:${requestScope.request}<br>
        <hr>
        使用struts标签
        request:<s:property value="#request.req"></s:property><br>
        session:<s:property value="#session.sess"></s:property><br>
        application:<s:property value="#application.app"></s:property><br>
        valuestack:${requestScope.request}<br>

    </body>
</html>



  • 在web/index.jsp中
<%--
  Created by IntelliJ IDEA.
  User: ����
  Date: 2022/9/13
  Time: 13:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>

<html>
    <head>
        <title>魔仙堡歌曲</title>
    </head>

    <body>

        <a href="${pageContext.request.contextPath}/test2/getServletAPIAction1.action">法一</a>
        <a href="${pageContext.request.contextPath}/pages/api/servlet_api_1.jsp">法二</a>

        <A href="${pageContext.request.contextPath}/test5/propertyAction.action">测试pp标签</A>

    </body>
</html>


  • 在src/struts.xml中
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 
    <package name="in" namespace="/test2" extends="struts-default">

        <action name="getServletAPIAction1" class="com.in.action.api.GetServletAPIAction1">
            <result type="dispatcher">/pages/api/servlet_api_1.jsp</result>
        </action>
    </package>
    
    <package name="in" namespace="/test5"  extends="struts-default">
        <action name="propertyAction" class="com.in.action.api.biaoqian.PropertyAction" method="execute">
            <result type="dispatcher" name="success">/pages/biaoqian/property.jsp</result>
        </action>
    </package>
</struts>

  • 在web/WEB-INF/web.xml中
<?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">
    <welcome-file-list>
        <welcome-file>index.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>

**3. 运行结果及报错内容 **

  • 运行index.jsp
    显示

    img

  • 点击法一显示

img

- 问题:找不到name为getServletAPIAction1的action

  • 点击法二显示

img


- 问题:没有值

  • 点击 测试pp标签 显示

img

  • ** 问题:找不到name为propertyAction的action**

**4. 我的解答思路和尝试过的方法 **

尝试过的方法
struts.xml文件配置了,action的名字没有出错
配置包名时,extends=“struts-default”没写错

1. 想要得到结果
得到值;可以访问到action**

  • 写回答

1条回答 默认 最新

  • MOMOGONG 2022-09-15 14:16
    关注

    已解决:

    在struts.xml文件中,如果有多个<package>标签,<package>标签的name属性不可以相同。
    (例如:代码中的<package>标签的name都为in,不可以。)
    (关于<package>标签作用的参考链接:https://www.cnblogs.com/carazk/p/6392294.html)
    

    参考链接:https://www.cnblogs.com/carazk/p/6392294.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 专家修改了标签 9月21日
  • 已结题 (查看结题原因) 9月16日
  • 已采纳回答 9月16日
  • 创建了问题 9月15日

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面