【问题标题】:Can't see spring controllers from XHTML file看不到 XHTML 文件中的弹簧控制器
【发布时间】:2013-10-20 12:46:54
【问题描述】:

我收到“无法解析变量 'indexController'”错误。

我的 xhtml 文件;

<?xml version='1.0' encoding='UTF-8' ?>
<!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:p="http://primefaces.org/ui">

<h:head>
    <f:facet name="first">
        <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
        <title>Title Goes Here</title>
    </f:facet>
</h:head>
<h:body>
    <h:form>
        <p:panel header="Send">
            <p:inputText value="Hi"></p:inputText>
            <p:commandButton value="Send" id="btnDisplay" actionListener="#{indexController}"/>
        </p:panel>

    </h:form>


</h:body>

我的控制器;

@Controller("indexController")
@Scope("session")
public class IndexController extends MainController {

private String name;

@Override
public void init() {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void destroy() {
    //To change body of implemented methods use File | Settings | File Templates.
}

public void sayHello(String name){
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

我的 applicationContext.xhtml 文件;

<?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.xsd
     http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">

 <context:component-scan base-package="tr.source.controllers"/>
 <context:annotation-config/>
</beans>

我的 web.xml 文件;

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.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>

【问题讨论】:

    标签: spring jsf jsf-2


    【解决方案1】:

    您的问题是 JSF 通过 CDI 或 ManagedBeans 解析 bean,而不是 Spring 管理的 bean。

    解决方案是为 Spring beans 配置一个 EL 名称解析器:

    把它放在你的 faces-config.xml 中

     <application>
       ...
       <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
     </application>
    

    有关更多信息,请参阅 Spring SpringBeanFacesELResolverjavadocs。

    【讨论】:

      猜你喜欢
      • 2016-12-23
      • 1970-01-01
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 2014-11-30
      相关资源
      最近更新 更多