yiqianyan 2017-04-30 14:40 采纳率: 100%
浏览 1073
已采纳

struts基础之指定method属性的动态方法调用

图片说明
HelloWorldAction.java:

 package com.imooc.action;

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

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
    private HttpServletRequest request;

    public String add(){
        return SUCCESS;
    }

    public String update(){
        return SUCCESS;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("执行action");
        return SUCCESS;
    }

    @Override
    public void setServletResponse(HttpServletResponse arg0) {

    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request=request;
    }

    @Override
    public void setServletContext(ServletContext arg0) {

    }

}

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>
    <package name="default" namespace="/" extends="struts-default">
    <action name="helloworld" class="com.imooc.action.HelloWorldAction">
            <result>/result.jsp</result>
        </action>

        <action name="addAction" method="add"
            class="com.imooc.action.HelloWorldAction">
            <result>/add.jsp</result>
        </action>

        <action name="updateAction" method="update"
            class="com.imooc.action.HelloWorldAction">
            <result>/update.jsp</result>
        </action>
        </package>

        </struts>

web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>test</display-name>
  <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>

add.jsp:

 <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<title></title>
</head>
<body>
    this is add.jsp
</body>
</html>

update.jsp:

 <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<title></title>
</head>
<body>
    this is update.jsp
</body>
</html>

result.jsp:

 <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<title></title>
</head>
<body>
    this is result.jsp
</body>
</html>

http://localhost:8080/test/helloworld.action能正常显示。
http://localhost:8080/test/addAction.action和http://localhost:8080/test/updateAction.action出现错误信息;
HTTP Status 404 - There is no Action mapped for namespace [/] and action name [helloworld_add] associated with context path [/test].

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [updateAction] associated with context path [/test].

  • 写回答

1条回答 默认 最新

  • devmiao 2017-04-30 15:29
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配