该问题已解决,链接地址:http://wlzjdm.iteye.com/blog/1521105
最近刚刚学完注解,就练习了一下下。我采用的是jsp页面里面的javascript代码访问action……
但是,我配置的注解无法将url和action进行映射,而且其中还出现了一个问题……
先看看我的那个jsp页面信息:
位置:WebRoot
内容:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ajaxAndStruts2.jsp' starting page</title>
<script type="text/javascript" src="../javascript/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function() {
$("#ok").click(function() {
$.post("./getLover.action",{"name":$("#name").val()},function(data){
},"xml");
});
});
</script>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<input type="text" id="name">
<input type="button" id="ok" value="select">
</body>
</html>
那么对应的Action的注解配置极其类内容为:
package com.dong.struts2WithOthers;
import org.apache.struts2.convention.annotation.Action;
import com.dong.util.Lover;
import com.opensymphony.xwork2.ActionSupport;
@Action(value = "getLover")
public class ByAjaxTest extends ActionSupport
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String execute() throws Exception
{
System.out.println("hello");
System.out.println(name);
if(name == "dongdong")
{
Lover lover = new Lover("xiaojing",21,"girl");
}
return "none";
}
}
出现的错误信息
2012-5-8 17:58:01 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find action or result
There is no Action mapped for namespace [/] and action name [getLover] associated with context path [/struts2]. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:500)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
我的分析与猜想:
我写的这个Action处理类是新建了一个包com.dong.struts2WithOthers;(原来练习的时候,所有的action都位于com.dong.action;这个包下面),当我吧这个类移动到原来的包下面的时候,可以正常运行,但是为何在新的包中就无法运行呢了。
我想这个是命名空间的问题,但是吧,尝试了好多次,都没有解决。不知道该如何的设定命名空间。
请求朋友指教帮忙。
我不明白为何提交请求后,它会自动的去com.dong.action;(原来练习struts2的包)中去找对应的Action
注:源代码并未完全实现我的目标,只是在进行链接测试的时候,遇到了问题,就停滞了下来。。。