开发者

Struts2+Jquery+JSON - some troubles

开发者 https://www.devze.com 2023-01-29 11:08 出处:网络
I need to load a part of code dinamically by using Jquery and Json. This is my struts.xml : <?xml version=\"1.0\" encoding=\"UTF-8\" ?>

I need to load a part of code dinamically by using Jquery and Json.

This is my 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>

this is my testAction class :

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;
    }
}

and i call that method with a submit :

<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>

Problems :

1 - Unfortunatly, when i try to Run the application, server log (tomcat) says :

Unable to load configuration
Error building results for action UserManager in namespace

regarding the line <action name="UserManager" class="model.UserManager">, and

There is no result type defined for type 'json' mapped with name 'success'.  Did you mean 'json'?

regarding <result>index.jsp</result>

2 - When i click on Example button, i don't get the Json element.

What am I wrong?

Cheers

UPDATE 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>

UPDATE 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(StandardCo开发者_如何学运维ntext.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


It seems you took this kind of example.

Try with 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>

And you have to include the json plugin in /WEB-INF/lib.

I use a different way.

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>

Method :

public String getJson() {
  String json = "({ 'var1': 'value1', 'var2': 'value2' })";
  inputStream = new ByteArrayInputStream(json.getBytes());
  return SUCCESS;
}

Update : I changed the struts.xml. The json-default package is defined in the plugin.

Update 2 : This setup worked for me.

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>

I had a problem with jsonplugin-0.34 I used struts2-json-plugin-2.1.8.jar instead. But the error I had has nothing to do with yours : It was "java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.TextUtils"

If the example button pop a download window, try this for the jsp page :

<%@ 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消