0

点击index.jsp页面中的URL后,返回消息

There is no Action mapped for namespace [/user] and action name [user!add] associated with context path [/Struts2]. - [unknown location]    

com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:722)

You are seeing this page because development mode is enabled. Development mode,
or devMode, enables extra debugging behaviors and reports to assist developers.
To disable this mode, set:
   struts.devMode=false
 in your WEB-INF/classes/struts.properties file. 

项目结构:

/Struts2/WebRoot/WEB-INF/web.xml

/Struts2/src/struts.xml

/Struts2/WebRoot/index.jsp

/Struts2/WebRoot/user_add_success.jsp

/Struts2/src/com/bjsxt/struts2/user/action/UserAction.java

/Struts2/src/com/bjsxt/struts2/user/model/User.java

我导入的 struts 包

  1. /Struts2/WebRoot/WEB-INF/lib/commons-fileupload-1.3.jar
  2. /Struts2/WebRoot/WEB-INF/lib/commons-io-2.2.jar
  3. /Struts2/WebRoot/WEB-INF/lib/commons-lang3-3.1.jar
  4. /Struts2/WebRoot/WEB-INF/lib/commons-logging-1.1.jar
  5. /Struts2/WebRoot/WEB-INF/lib/freemarker-2.3.19.jar
  6. /Struts2/WebRoot/WEB-INF/lib/javassist-3.11.0.GA.jar
  7. /Struts2/WebRoot/WEB-INF/lib/ognl-3.0.6.jar
  8. /Struts2/WebRoot/WEB-INF/lib/struts2-core-2.3.16.jar
  9. /Struts2/WebRoot/WEB-INF/lib/xwork-core-2.3.16.jar

/Struts2/WebRoot/WEB-INF/web.xml _

   <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Struts Blank</display-name>
    <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>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

/Struts2/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.devMode" value="true" />
<package name="user" extends="struts-default" namespace="/user">      
    <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
        <result>/user_add_success.jsp</result>
    </action>
</package>
</struts>

/Struts2/WebRoot/index.jsp _

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"  pageEncoding="GB18030"%>
<% 
   String path = request.getContextPath();
   String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<head>
<base href="<%=basePath %>"/>
<title>Insert title here</title>
</head>
<body>
   use Domain Model to recieve param <a href="user/user!add?user.name=a&user.age=8">add</a>
</body>
</html>
 

/Struts2/WebRoot/ user_add_success.jsp

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
     User Add Success!
</body>
</html>

/Struts2/src/com/bjsxt/struts2/user/action/UserAction.java _

package com.bjsxt.struts2.user.action;
import com.bjsxt.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private User user;
//private UserDTO userDTO;
public String add() {
    System.out.println("name=" + user.getName());
    System.out.println("age=" + user.getAge());
    return SUCCESS;
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
}

/Struts2/src/com/bjsxt/struts2/user/model/User.java _

package com.bjsxt.struts2.user.model;
public class User {
private String name;
private int age;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
}
4

2 回答 2

1

代替

<a href="user/user!add?user.name=a&user.age=8">add</a>

利用

<s:url var="myUrl" namespace="/user" action="user" method="add">
    <s:param name="user.name" value="%{'a'}" />
    <s:param name="user.age"  value="%{'8'}" />
</s:url>
<s:a href="%{myUrl}">add</s:a>
于 2014-02-28T16:34:15.260 回答
0

如果您在 struts.xml 文件中遇到问题,此异常实际上会出现。更改您的 struts xml 结果位置

<result>/user_add_success.jsp</result>

<result>user_add_success.jsp</result>

或尝试将您的 jsp 文件中的操作名称更改为 .user.action但我确信上述操作会为您完成。这肯定是由于 struts.xml 文件中的问题而发生的。检查通过。

于 2014-03-03T04:47:54.723 回答