问题背景:在IDEA使用Struts2,运行后获取不到传入的值或者键值信息没有传入到后台,以及There is no Action mapped for action name propertyAction.
用代码块功能插入代码
- 在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
显示点击法一显示
- 问题:找不到name为getServletAPIAction1的action
- 点击法二显示
- 问题:没有值
- 点击 测试pp标签 显示
- ** 问题:找不到name为propertyAction的action**
**4. 我的解答思路和尝试过的方法 **
尝试过的方法 |
---|
struts.xml文件配置了,action的名字没有出错 |
配置包名时,extends=“struts-default”没写错 |
1. 想要得到结果
得到值;可以访问到action**