2013-07-16 98 views
0

Im試着將名爲「userVO」的bean映射到帶有註釋的安全控制器,它在我通過xml配置映射時起作用,但當我使用註釋時,我不斷收到以下錯誤自動裝配外部bean不起作用

警告:/login.xhtml @ 22,37值= 「#{} securityController.userVO.vc_name」:目標unreacheable, 'userVO' 返回null

我必須說UserVO是在不同的項目,我自己也,並且它的依賴性由maven處理

以下是我的spring-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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

<import resource="mapper-beans.xml" /> 


<bean name="userVO" id="userVO" class="rst.core.security.UserVO" 
    scope="session" /> 

    <context:annotation-config /> 
    <context:component-scan base-package="rst.core.security" /> 
     <context:component-scan base-package="rst.controller" /> 

</beans> 

以下是映射到userVO其不工作控制器:

@ManagedBean 
@RequestScoped 
@Controller 
public class SecurityController { 

public static Logger log; 

private WebFacade webFacade; 

@Autowired 
private UserVO userVO; 

public SecurityController() { 
     log = LoggerFactory.getLogger(this.getClass()); 
     log.debug("Creating SecurityController."); 
} 

public String login() { 

    UserVO user= webFacade.validateUser(getUserVO()); // method to search the db 
    if (user!= null) { 
     return "accessGranted"; 
    } else { 
     return "accessDenied"; 
    } 
} 

//getters and setters.... 

} 

而且這是在登錄屏幕觸發錯誤:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1  /DTD/xhtml1-transitional.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"> 

    <h:head> 

    </h:head> 

    <h:body> 
    <h:form id="form"> 

    <p:panel id="panel" header="New Person"> 

     <h:panelGrid columns="3"> 
      <h:outputLabel for="name" value="Name: *" /> 
      <p:inputText id="name" value="#{securityController.userVO.vc_name}" 
       label="name" required="true"/> 

      <h:outputLabel for="password" value="Password: *" /> 
      <p:inputText id="password" value="#{securityController.userVO.vc_password}" 
       required="true" label="Password" type="password"/> 

      </h:panelGrid> 

      <p:commandButton id="btn" value="Login" update="panel" 
      action="#{securityController.login}"/> 
    </p:panel> 

    </h:form> 

    </h:body> 
    </html> 

而下面我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-app_3_0.xsd"> 
    <display-name>rst-web</display-name> 
    <!---+++++++++++++++++++++++++++++++++++++++++++++++++Spring++++++++++++++++++++++++++++++++++++++++++--> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring-beans.xml 
</param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 
    <!-- ++++++++++++++++++++++++ JSF +++++++++++++++++++++++++++++++++++++++++++++++++++ --> 
    <servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <context-param> 
     <param-name>javax.faces.DEFAULT_SUFFIX</param-name> 
     <param-value>.xhtml</param-value> 
    </context-param> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>/login.xhtml</welcome-file> 
    </welcome-file-list> 

    </web-app> 

回答

0

securityController如何重新解決了?它被標記爲@ManagedBean,這使得它對JSF可見,另一方面JSF在實例化SecurityManager時不會識別spring的@Autowired

如果你想堅持到春天,擺脫@ManagedBean,改變@RequestScoped到@Scope(「請求」),並確保你已經設置了ELResolver正確的面孔,配置:

<faces-config> 
    <application> 
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
    ... 
    </application> 
</faces-config> 
+0

不幸的是,當我刪除'@ManagedBean'註釋時,我得到以下錯誤: 警告:/login.xhtml @ 22,37 value =「#{securityController.userVO.vc_name}」:目標無法訪問,標識符'securityController'已解析爲null – pablocmg

+0

這證實了我的懷疑,您沒有正確配置JSF-spring集成。請看看答案的其餘部分。 – mrembisz

+0

哦,但org.springframework.web.jsf.el.SpringBeanFacesELResolver已經配置,如果多數民衆贊成你是什麼意思... – pablocmg