当前位置: 首页 > 知识库问答 >
问题:

JSF 2.2目标不可达,标识符[whater]解析为空

蒋何平
2023-03-14

我正在尝试将JSF2.2与Spring4和Spring Data JPA(Hibernate)集成。按下“保存”按钮时出现以下异常:

javax.el.PropertyNotFoundException: /index.xhtml @14,66 value="#{registerEmployee.employee.employeeName}": Target Unreachable, identifier [registerEmployee] resolved to null

Eclipse:Oxygen.3A释放版(4.7.3A)

org.springframework.orm.hibernate5.LocalSessionFactoryBean
<property name="mappingResources">
  <list>
    <value>domain-classes.hbm.xml</value>
  </list>
</property>

在使用@component时,导入为:org.springframework.stereotype.component;

在使用@transactional时,导入为:org.springframework.transaction.annotation.transactional;

在使用@managedbean时,导入为:javax.faces.bean.managedbean;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Component;

@Entity
@Table(name = "employee")
@Component
public class Employee{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "EMP_ID")
private long employeeId;

@Column(name = "EMP_NAME")
private String employeeName;

@Column(name = "EMP_HIRE_DATE")
private Date employeeHireDate;

@Column(name = "EMP_SALARY")
private double employeeSalary;

public long getEmployeeId() {
    return employeeId;
}

public void setEmployeeId(long employeeId) {
    this.employeeId = employeeId;
}

public String getEmployeeName() {
    return employeeName;
}

public void setEmployeeName(String employeeName) {
    this.employeeName = employeeName;
}

public Date getEmployeeHireDate() {
    return employeeHireDate;
}

public void setEmployeeHireDate(Date employeeHireDate) {
    this.employeeHireDate = employeeHireDate;
}

public double getEmployeeSalary() {
    return employeeSalary;
}

public void setEmployeeSalary(double employeeSalary) {
    this.employeeSalary = employeeSalary;
}
}
@ManagedBean(name = "registerEmployee")
@SessionScoped
public class RegisterEmployee implements Serializable {

@ManagedProperty("#{employeeService}")
private EmployeeService employeeService;

@Autowired
private Employee employee;

public EmployeeService getEmployeeService() {
    return employeeService;
}

public void setEmployeeService(EmployeeService employeeService) {
    this.employeeService = employeeService;
}

public Employee getEmployee() {
    return employee;
}

public void setEmployee(Employee employee) {
    this.employee = employee;
}

public String register() {
    // Calling Business Service
    employeeService.register(employee);
    // Add message
    FacesContext.getCurrentInstance().addMessage(null, 
            new FacesMessage("The Employee "+this.employee.getEmployeeName()+" Is Registered Successfully"));
    return "";
   }
}
public interface EmployeeRepository extends JpaRepository<Employee, Integer>{ 

}
@Component
public class EmployeeService implements Serializable {

@Autowired
private EmployeeRepository employeeRepository;

public EmployeeRepository getEmployeeRepository() {
return employeeRepository;
}

public void setEmployeeRepository(EmployeeRepository employeeRepository) {
    this.employeeRepository = employeeRepository;
}

@Transactional
public void register(Employee employee) {
    employeeRepository.save(employee);
}

}

pom.xml

<repositories>
  <repository>
    <id>prime-repo</id>
    <name>PrimeFaces Maven Repository</name>
    <url>http://repository.primefaces.org</url>
    <layout>default</layout>
  </repository>
</repositories>

<dependencies>

    <!-- Expression Language -->
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
    </dependency>

    <!-- JSTL Library -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- Java Server Faces Implementation -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.4</version>
    </dependency>

    <!-- Java Server Faces API -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.4</version>
    </dependency>       

    <!-- Primefaces Version 5 -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.0</version>
    </dependency>

    <!-- Primefaces Theme Library -->
    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>blitzer</artifactId>
        <version>1.0.10</version>
    </dependency>

    <!-- Hibernate Core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.10.Final</version>
    </dependency>

    <!-- Hibernate Entity Manager -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.10.Final</version>
    </dependency>

    <!-- MySQL Connector Java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.44</version>
    </dependency>

    <!-- Spring Context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.16.RELEASE</version>
    </dependency>

    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.3.16.RELEASE</version>
    </dependency>

    <!-- Spring Web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.16.RELEASE</version>
    </dependency>

    <!-- Spring Data JPA -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.0.5.RELEASE</version>
    </dependency>

    <!-- LOG4J -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>   

   <!-- JUnit -->  
   <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
     <finalName>Nova</finalName>
        <plugins>
           <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <configuration>
                 <source>1.8</source>
                <target>1.8</target>
              </configuration>
          </plugin>
      </plugins>

</build>

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    ">

<!--  Enable Spring annotations  -->
<context:annotation-config />

<!-- Scans for repositories that will be auto-registered -->
<jpa:repositories base-package="where.repositories.are.located" />

<!-- Scans for components (beans) which are managed by Spring -->
<context:component-scan base-package="where.services.are.located, where.models.are.located" />

<!-- MySQL Datasource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/nova" />
    <property name="username" value="username" />
    <property name="password" value="supersecretpassword" />
</bean>

<!-- EntityManagerFactory and JPAVendorAdapter -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="where.models.are.located"
        p:dataSource-ref="dataSource"
        >
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="false"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
        </bean>
    </property>
</bean>

<!-- Transaction Manager -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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"
    version="2.2">
<application>
   <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
     version="3.1">

<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>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

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

<context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>WEB-INF/faces-config.xml</param-value>
</context-param>

<context-param>
   <param-name>javax.faces.PROJECT_STAGE</param-name>
   <param-value>Development</param-value>
</context-param>

<context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>

xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
   <script name="jquery/jquery.js" library="primefaces"></script>
   <title>Register Employee</title>
</h:head>
<h:form>
    <p:growl id="messages"></p:growl>
    <p:panelGrid columns="2">
       <p:outputLabel value="Enter Employee Name:"></p:outputLabel>
       <p:inputText value="#{registerEmployee.employee.employeeName}"> 
  </p:inputText>
    <p:outputLabel value="Enter Employee Hire Date:"></p:outputLabel>
    <p:calendar value="#{registerEmployee.employee.employeeHireDate}"> 
  </p:calendar>
    <p:outputLabel value="Enter Employee Salary:"></p:outputLabel>
    <p:inputText value="#{registerEmployee.employee.employeeSalary}"> 
  </p:inputText>                        
  </p:panelGrid>
  <p:commandButton value="Register" action="#{registerEmployee.register}" 
  update="messages"></p:commandButton>
  </h:form>
  </html>

共有1个答案

屠坚壁
2023-03-14

@autowired或@inject用@entity注释的类是一种不好的模式。

 类似资料:
  • 目标不可达,标识符'bean'解析为null 目标不可达,'Entity'返回null 目标不可达,'null'返回null 目标无法到达,“0”返回null 目标不可达,'bracketsuffix'返回null 它们都是什么意思?它们是如何造成的,应该如何解决?

  • 我目前正在为JSF网站开发一个sql类。但我在尝试访问我的类时遇到了一个恼人的错误: null null null null 这里是stacktrace:

  • 首先,创建到服务器的UDP连接: 没有任何直接连接(没有ip,没有端口,只是随机端口上的endpoint)。 我使用直接在服务器的ip和端口上发送一些数据包到服务器,并获取有关另一个对等方的信息(远程ip,用于连接到服务器的端口 现在,我试图通过使用服务器连接在对等方的IP:端口上发送简单的UDP数据包(但再次使用直接)来解决问题。我知道第一个数据包总是被丢弃,所以我以100ms的间隔发送它50,

  • 问题内容: 我刚刚将Xcode更新为6.3版本。 除了我必须进行的所有修复(主要是)外,我还有一些警告刚刚出现(这很奇怪,因为它声明自iOS 8.0以来)。 我的密码 现在显示警告 好吧,那么我会改变的 现在我得到一个错误,说: 我感到困惑,有人可以向我解释什么地方出了问题以及如何解决? 问题答案: 使用。NSCalendar是一个本机的Objective-C框架,错误消息并不迅速。

  • 我有一个非常简单的项目。它是使用Xcode 8.0在Swift 3.0上编写的命令行工具。这个程序是: 这工作得很好,并在控制台中打印“a”,但让我们做这个程序更复杂: 和线 被标记为错误 使用未解析的标识符“b” 我们可以让事情变得更简单: 再一次,线 被标记为错误 未解析标识符“a”的使用 我不是新手,我明白我可以很容易地修复这个错误,就像把所有变量放在程序的开头一样。问题是:为什么会发生这种

  • 问题内容: 这里有什么问题? 这抱怨: 问题答案: 将你的代码放在一个方法中。 尝试这个: 然后从你的IDE“运行”该类