yiqianyan
2017-04-30 14:40
采纳率: 90.9%
浏览 1.1k
已采纳

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
    已采纳
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题