public class WordProjectinfoAction extends BaseAction implements ServletConfigAware {
private ServletConfig servletconfig;
public void setServletConfig(ServletConfig servletconfig) {
this.servletconfig=servletconfig;
}。
}
这里我实现了 ServletConfigAware 我用的是smartuplad上传 需要初始化servletconfig
而我实现了这个接口 怎么报错 说servletconfig 为null 我该则么解决啊
谢谢大家
<prop key="someName1">someValue1</prop>
<prop key="someName2">someValue2</prop>
</util:properties>
把myConfig注入到 Action里面去,不也可以吗?
看来兄弟你是没有理解 ServletConfigAware 接口的作用啊。
<util:properties id="myConfig">
<prop key="someName1">someValue1</prop>
<prop key="someName2">someValue2</prop>
</util:properties>以上定义的就是一个java.util.Properties的bean;名字是“myConfig”。
下面是一个Action,你不是可以拿到你在配置文件里写的东西了吗?public class SomeAction extends ActionSupport { private java.util.Properties props; public void execution () {
// ... String someValue = props.get("someKey");
} @Resource(name = "myConfig")
public void setProps(java.util.Properties props) {
this.props = prps;
}
}
package com.ncu.hrm.action;import java.util.Map;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.web.context.ServletContextAware;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware, ServletContextAware {
protected ActionContext actionCtx = ActionContext.getContext(); protected HttpServletRequest request; protected HttpServletResponse response; protected ServletContext application; protected Map<String, Object> session; public void setServletRequest(HttpServletRequest request) {
this.request = request;
} public void setServletResponse(HttpServletResponse response) {
this.response = response; } public void setServletContext(ServletContext application) {
this.application = application; } public void setSession(Map<String, Object> session) {
this.session = session; } protected void put(String name, Object value) {
actionCtx.put(name, value);
} protected Object get(String name) {
return actionCtx.get(name);
}
}
ApplicationContent.xml中配置一个bean
Action中设置一个属性 生成get set就会自动注入了
或者用@Resource
adviceManager = (AdviceManager) context.getBean("adviceManager");
lz 可以读取一下配置文件。。