在backingbean中注入Spring服务
我正在尝试用netbeans 7.4、SPRING 3.2.3、JSF 2.2、Primefaces 3.5和Glassfish 4.0构建一个web 我的目标是创建一个非常简单的项目,将服务注入到支持bean中。 到目前为止,我已经用这两个框架在netbeans中创建了一个新项目。以下是我的项目中的文件: 项目树在backingbean中注入Spring服务,spring,web-services,jsf,primefaces,Spring,Web Services,Jsf,Primefaces,我正在尝试用netbeans 7.4、SPRING 3.2.3、JSF 2.2、Primefaces 3.5和Glassfish 4.0构建一个web 我的目标是创建一个非常简单的项目,将服务注入到支持bean中。 到目前为止,我已经用这两个框架在netbeans中创建了一个新项目。以下是我的项目中的文件: 项目树 webApplication1 ├ Web Pages │ ├ WEB-INF │ │ ├ applicationContext.XML │
webApplication1
├ Web Pages
│ ├ WEB-INF
│ │ ├ applicationContext.XML
│ │ ├ dispatcher-servlet.XML
│ │ ├ faces-config.XML
│ │ └ web.XML
│ └ index.XHTML
├ source packages
│ └ controladores
│ ├ IndexBB.java
│ ├ Servicio.java
│ └ ServicioImpl.java
├ Test Packages
├ Libraries
├ Test Libraries
└ Configuration Files
Index.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:body>
<h:form>
<p:inputText id="aInput" value="#{indexBB.mensaje}" />
<p:commandButton id="btnTest" actionListener="#{indexBB.getServiceSMS}" value="Test" update="aInput" />
</h:form>
</h:body>
</f:view>
</html>
Servicio.java
package controladores;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class IndexBB implements Serializable{
@ManagedProperty(value="#{rfService}")
private Servicio elServicio;
private String mensaje="a default SMS";
public IndexBB() {
}
public String getMensaje() {
return this.mensaje;
}
public void setMensaje(String mensaje) {
this.mensaje = mensaje;
}
public Servicio getElServicio() {
return this.elServicio;
}
public void setElServicio(Servicio elServicio) {
this.elServicio = elServicio;
}
public void getServiceSMS() {
this.mensaje=elServicio.getSMS();
}
}
package controladores;
public interface Servicio {
public String getSMS();
}
package controladores;
import org.springframework.stereotype.Service;
@Service(value="rfService")
public class ServicioImpl implements Servicio{
@Override
public String getSMS() {
return "a message generated by a service";
}
}
ServicioImpl.java
package controladores;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class IndexBB implements Serializable{
@ManagedProperty(value="#{rfService}")
private Servicio elServicio;
private String mensaje="a default SMS";
public IndexBB() {
}
public String getMensaje() {
return this.mensaje;
}
public void setMensaje(String mensaje) {
this.mensaje = mensaje;
}
public Servicio getElServicio() {
return this.elServicio;
}
public void setElServicio(Servicio elServicio) {
this.elServicio = elServicio;
}
public void getServiceSMS() {
this.mensaje=elServicio.getSMS();
}
}
package controladores;
public interface Servicio {
public String getSMS();
}
package controladores;
import org.springframework.stereotype.Service;
@Service(value="rfService")
public class ServicioImpl implements Servicio{
@Override
public String getSMS() {
return "a message generated by a service";
}
}
applicationContext.XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.xhtml">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/"
p:suffix=".xhtml" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
在我看来,服务没有被正确注入,所以当我按下按钮时,它是空的
我在网上搜索,没有结果(我找到的大部分代码都不起作用)。
有配置Spring经验的人可以看看我的项目吗?
提前谢谢 您有3个问题
首先,您有一个JSF环境中不需要的DispatcherServlet
。删除它,它的映射。您不需要所有控制器的东西,因为它们是由JSF处理的
第二,你有一个@服务,但是没有任何东西可以接收这个bean。在applicationContext.xml
中添加组件扫描
<context:component-scan base-package="controladores" />
第三,您的托管bean的setter错误。您必须有一个与@ManagedProperty
注释中的值匹配的setter,因此在您的情况下,应该有一个setRfService(..)
方法 非常感谢迪纳姆先生!
你的答案非常接近,我只需要谷歌一下!
作为对您所有评论的补充,我在“beans”开始标记内的应用程序上下文中添加了以下行:
xmlns:context="http://www.springframework.org/schema/context"
这个在xsi:schemaLocation中
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
我还删除了WEB.XML中声明dispatcher的行:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
调度员
org.springframework.web.servlet.DispatcherServlet
2.
这是:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
调度员
*.xhtml
谢谢你 我已删除DispatcherServlet,将“xmlns:context=”“”和您的行添加到applicationContext。我还调用了服务“ElServiceIO”,但它仍然不工作。加载应用程序时Stacktrace:Exception:java.lang.IllegalStateException:ContainerBase.addChild:start:org.apache.catalina.LifecycleException:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:ServletContext资源xml文档中的第25行[/WEB-INF/applicationContext.xml]无效;嵌套异常为org.xml.sax.SAXParseException;行号:25;列号:60;cvc复杂类型。2.4.c:El-comodín Concurrence es estricto,pero no se ha encontrado ninguna Declarationón para El-elemento“context:component scan”。有什么问题吗?