我需要使用Jquery和Json加载一部分代码。
这是我的 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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="model" extends="struts-default">
<action name="UserManager" class="model.UserManager">
<result>index.jsp</result>
</action>
</package>
<package name="test" extends="json-default">
<action name="giveMeJsonData" class="test.testAction" method="myFunction">
<result type="json"><param name="root">jsonData</param></result>
</action>
</package>
</struts>
这是我的 testAction 类:
package test;
import com.opensymphony.xwork2.ActionSupport;
import java.util.LinkedHashMap;
public class testAction extends ActionSupport {
private LinkedHashMap<String, String> jsonData;
public LinkedHashMap<String, String> getJsonData() { return jsonData; }
public void setJsonData(LinkedHashMap<String, String> jsonData) { this.jsonData = jsonData; }
@Override
public String execute() throws Exception {
return SUCCESS;
}
public String myFunction() {
jsonData = new LinkedHashMap<String, String>();
jsonData.put("My name is", "marco");
return SUCCESS;
}
}
我用提交方法调用该方法:
<s:div id="my_result" >
<s:property value="jsonData" />
</s:div>
<s:form action="giveMeJsonData" theme="simple">
<sj:submit targets="my_result" value="Example" />
</s:form>
问题:
1 - 不幸的是,当我尝试运行应用程序时,服务器日志(tomcat)说:
Unable to load configuration
Error building results for action UserManager in namespace
关于第<action name="UserManager" class="model.UserManager">
行和
There is no result type defined for type 'json' mapped with name 'success'. Did you mean 'json'?
关于<result>index.jsp</result>
2 - 当我点击Example按钮时,我没有得到Json元素。
我错了什么?
干杯
更新1
<package name="json-default" extends="struts-default">
<result-types>
<result-type name="json" class="com.googlecode.jsonplugin.JSONResult"/>
</result-types>
</package>
<package name="test" extends="json-default">
<action name="giveMeJsonData" class="test.testAction" method="myFunction">
<result type="json"><param name="root">jsonData</param></result>
</action>
</package>
更新2
9-dic-2010 13.49.35 org.apache.catalina.core.StandardContext filterStart
GRAVE: Exception starting filter struts2
Unable to load configuration. - action - file:/C:/Users/djfonplaz/Documents/Stage/Struts2/build/web/WEB-INF/classes/struts.xml:11:62
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3838)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4488)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:637)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:563)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:498)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: Unable to load configuration. - action - file:/C:/Users/djfonplaz/Documents/Stage/Struts2/build/web/WEB-INF/classes/struts.xml:11:62
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
... 29 more
Caused by: Error building results for action UserManager in namespace - action - file:/C:/Users/djfonplaz/Documents/Stage/Struts2/build/web/WEB-INF/classes/struts.xml:11:62
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:367)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:468)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:264)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:193)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
... 31 more
Caused by: There is no result type defined for type 'json' mapped with name 'success'. Did you mean 'json'? - result - file:/C:/Users/djfonplaz/Documents/Stage/Struts2/build/web/WEB-INF/classes/struts.xml:12:35
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildResults(XmlConfigurationProvider.java:621)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:365)
... 36 more
9-dic-2010 13.50.23 org.apache.catalina.core.StandardContext filterStart
GRAVE: Exception starting filter struts2
Unable to load configuration. - action - file:/C:/Users/djfonplaz/Documents/Stage/Struts2/build/web/WEB-INF/classes/struts.xml:11:62
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3838)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4488)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:637)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:521)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1359)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:297)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761)
at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1500)
at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:849)
at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:351)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:199)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:558)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:619)
Caused by: Unable to load configuration. - action - file:/C:/Users/djfonplaz/Documents/Stage/Struts2/build/web/WEB-INF/classes/struts.xml:11:62
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
... 41 more
答案 0 :(得分:9)
您似乎采用了this类示例。
尝试使用此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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="test" extends="json-default">
<action name="giveMeJsonData" class="test.testAction" method="myFunction">
<result type="json"><param name="root">jsonData</param></result>
</action>
</package>
</struts>
您必须在/WEB-INF/lib
中加入json plugin。
我用不同的方式。
struts.xml:
<action name="getjson" class="myclass" method="getJson">
<result name="success" type="stream">
<param name="contentType">application/json</param>
<param name="inputName">inputStream</param>
</result>
</action>
方法:
public String getJson() {
String json = "({ 'var1': 'value1', 'var2': 'value2' })";
inputStream = new ByteArrayInputStream(json.getBytes());
return SUCCESS;
}
更新
我改变了struts.xml。 json-default
包在插件中定义。
更新2: 这个设置对我有用。
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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="TestJson" extends="json-default">
<action name="giveMeJsonData" class="test.testAction" method="myFunction">
<result type="json"><param name="root">jsonData</param></result>
</action>
</package>
</struts>
我遇到了jsonplugin-0.34的问题,而是使用了struts2-json-plugin-2.1.8.jar。 但是我犯的错误与你的错误无关:它是“java.lang.ClassNotFoundException:com.opensymphony.xwork2.util.TextUtils”
如果示例按钮弹出下载窗口,请尝试使用jsp页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<sj:head/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Json Test</title>
</head>
<body>
<s:div id="my_result" >
<s:property value="jsonData" />
</s:div>
<sj:a targets="my_result" href="giveMeJsonData">Example</sj:a>
</body>
</html>