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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀