0

我试图将名为“userVO”的bean映射到带有注释的安全控制器,当我通过xml配置映射它时它可以工作,但是当我使用注释时我不断收到以下错误

警告:/login.xhtml @22,37 value="#{securityController.userVO.vc_name}":目标无法访问,'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>
4

1 回答 1

0

securityController 是如何解决的?它被标记为@ManagedBean对 JSF 可见,另一方面 JSF@Autowired在实例化 SecurityManager 时不会识别 spring。

如果您想坚持使用 spring,请摆脱@ManagedBean,将 @RequestScoped 更改为 @Scope("request") 并确保您在 faces-config 中正确设置了 ELResolver:

<faces-config>
  <application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    ...
  </application>
</faces-config>
于 2013-07-16T07:31:16.443 回答