我已經創建了一個用於測試struts2依賴注入(@Inject
)的應用程序。注射在很多方面都很好,除了我定義了網絡服務操作的球衣休息服務課程之外。Struts2 @Inject不能在澤西島工作其他webservices
我越來越喜歡如下圖所示
Sep 22, 2014 8:48:50 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at usermodules.services.UserModulesServices.userName(UserModulesServices.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
誰能告訴我一些解決方案,這
struts2.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>
<bean class="net.viralpatel.struts2.action.MoreServiceImpl" name="services" />
<constant name="struts.action.excludePattern" value="/rest/.*" />
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<action name="login"
class="net.viralpatel.struts2.action.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>
</action>
</package>
</struts>
UserModulesServices.java
例外import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import net.viralpatel.struts2.action.MoreServiceImpl;
import com.opensymphony.xwork2.inject.Inject;
@Path("/users")
public class UserModulesServices {
@Inject("services")
public MoreServiceImpl moreServiceImpl;
public MoreServiceImpl getMoreServiceImpl() {
return moreServiceImpl;
}
public void setMoreServiceImpl(MoreServiceImpl moreServiceImpl) {
this.moreServiceImpl = moreServiceImpl;
}
@GET
@Path("/name/{i}")
@Produces(MediaType.TEXT_PLAIN)
public String userName(@PathParam("i") String i) {
System.out.println("name::::::::" + moreServiceImpl.validate());
return "{\"name\":\"" + i + "\"}";
}
}
MoreServiceImpl.java
package net.viralpatel.struts2.action;
public class MoreServiceImpl implements MoreServices{
@Override
public String validate() {
return "testing";
}
}
閱讀評論[此問題](http://stackoverflow.com/q/17244036/1654265)。你不應該使用內部的XWork注入機制,它充滿了新的東西來實現相同的目標,更好。 – 2014-09-22 14:15:11
但@Inject在所有其他類中工作 – 2014-09-22 14:18:26
另外,如果您在任何地方使用Scriptlets,它們都可以工作。此外,如果您使用Visual Basic 6,它的工作。但你不應該。 – 2014-09-22 14:23:34