我已经创建了一个用于测试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