点击index.jsp
页面中的网址后,会返回消息
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包
- /Struts2/WebRoot/WEB-INF/lib/commons-fileupload-1.3.jar
- /Struts2/WebRoot/WEB-INF/lib/commons-io-2.2.jar
- /Struts2/WebRoot/WEB-INF/lib/commons-lang3-3.1.jar
- /Struts2/WebRoot/WEB-INF/lib/commons-logging-1.1.jar
- /Struts2/WebRoot/WEB-INF/lib/freemarker-2.3.19.jar
- /Struts2/WebRoot/WEB-INF/lib/javassist-3.11.0.GA.jar
- /Struts2/WebRoot/WEB-INF/lib/ognl-3.0.6.jar
- /Struts2/WebRoot/WEB-INF/lib/struts2-core-2.3.16.jar
- /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的/用户/操作/的 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.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;
}
}
答案 0 :(得分: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>
答案 1 :(得分:0)
此异常实际上如果您在struts.xml文件中遇到问题。 更改您的struts xml结果位置 从
<result>/user_add_success.jsp</result>
到
<result>user_add_success.jsp</result>
或
尝试将jsp文件中的操作名称更改为user.action
。但我确信上面的操作名称会为你完成。由于struts.xml文件中的问题,肯定会发生这种情况。检查通过。