定义View Params之间有什么区别:
<f:metadata>
<f:viewParam name="id" value="#{someBean.id}"/>
</f:metadata>
Run Code Online (Sandbox Code Playgroud)
并在ManagedBean中定义属性,如下所示:
@ManagedProperty(value = "#{param.id}")
private Integer id;
Run Code Online (Sandbox Code Playgroud) 我正在尝试将DAO注入托管属性.
public class UserInfoBean {
private User user;
@ManagedProperty("#{userDAO}")
private UserDAO dao;
public UserInfoBean() {
this.user = dao.getUserByEmail("test@gmail.com");
}
// Getters and setters.
}
Run Code Online (Sandbox Code Playgroud)
在创建bean之后注入DAO对象,但它null
在构造函数中并因此导致NullPointerException
.如何使用注入的托管属性初始化托管bean?
jsf constructor nullpointerexception managed-bean managed-property
我是JSF的新手并且想知道:
如果我有一个处理给定页面的所有工作的控制器和一个包含所述页面的所有数据的bean,那么必须同时拥有
@ManagedProperty(value="#{myBean}")
Run Code Online (Sandbox Code Playgroud)
控制器上的注释和
@ManagedBean(name="myBean")
@SessionScoped
Run Code Online (Sandbox Code Playgroud)
表单bean上的注释?
如果我不能将@ManagedProperty注释与@Named一起使用,因为@ManagedProperty在CDI(?)中不起作用,那么如何将URL中的params传递给facelets客户端?在我的代码中,我想通过"后退"和"前进"按钮将javax.mail.getMessageNumber()传递给details.xhtml.
我知道应该使用@Inject,但是注入的是什么,请问?
从glassfish日志中,id总是0,这很奇怪.即使单击"前进",无论单击按钮多少次,ID都不会超过1.当然,这仅仅是问题的症状.当然,所需的输出是前进到下一个消息.
也许将Message或者至少是int放入会话中?
客户端如此:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./template.xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core">
<ui:define name="top">
<h:form>
<h:form>
<h:outputLink id="link1" value="detail.xhtml">
<f:param name="id" value="#{detail.back()}" />
<h:outputText value="back" />
</h:outputLink>
</h:form>
</h:form>
<h:form>
<h:outputLink id="link1" value="detail.xhtml">
<f:param name="id" value="#{detail.forward()}" />
<h:outputText value="forward" />
</h:outputLink>
</h:form>
</ui:define>
<ui:define name="content">
<h:outputText value="#{detail.content}"></h:outputText>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
和bean一样:
package net.bounceme.dur.nntp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedProperty;
import javax.inject.Named;
import javax.mail.Message;
@Named
@RequestScoped
public class Detail …
Run Code Online (Sandbox Code Playgroud) 我有一个CDI托管bean,我想将请求参数设置为托管属性:
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
@Named
@RequestScoped
public class ActivationBean implements Serializable {
@ManagedProperty(value="#{param.key}")
private String key;
@ManagedProperty(value="#{param.id}")
private Long id;
// Getters+setters
Run Code Online (Sandbox Code Playgroud)
domain/activate.jsf?key=98664defdb2a4f46a527043c451c3fcd&id=5
但是,URL是永远不会设置和保留的属性null
.
这是怎么造成的,我该如何解决?
我知道我可以从ExternalContext
下面手动抓取它们:
Long id = Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"), 10);
String key = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("key");
Run Code Online (Sandbox Code Playgroud)
但是,我宁愿使用注射剂.
jsf cdi managed-bean http-request-parameters managed-property
我正在使用类的层次结构,我最好尝试做的是@ManagedBean
继承一个具有@ManagedProperty
成员和@PostConstruct
方法的类.
具体来说,这会有用吗?:
public class A {
@ManagedProperty
private C c;
@PostConstruct
public void init() {
// Do some initialization stuff
}
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
}
@ManagedBean
@SessionScoped
public class B extends A {
// Content...
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
通过定义managedproperty,在另一个中注入一个managedbean会有些麻烦.我正在谷歌搜索和堆栈溢出现在3天,但没有结果......
我正在使用eclipse 4.2开发并部署到集成的Tomcat 7
那么,任何人都可以告诉我,为什么我的财产是空的?
的pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.0.5.RELEASE</spring.version>
<java.version>1.6</java.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
web.xml中
<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">
<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>
Run Code Online (Sandbox Code Playgroud)
我在applicationContext中设置了bean来扫描@Autowired注释.(是的,我在applicationContext中没有bean的情况下尝试过它,但是也不会设置ManagedProperty.)
applicationContext.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="myPackage" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="myPackage.dao.UserDao" id="userDao" />
<bean class="myPackage.dao.WorldDao" id="worldDao" />
<bean class="myPackage.dao.BuildingTypeDao" id="buildingTypeDao" />
<bean class="myPackage.dao.BuffTypeDao" …
Run Code Online (Sandbox Code Playgroud) 我尝试学习JSF并遇到与ManagedProperty相关的问题.但是我试图使用它,它总是失败 - null异常指针.我做错了什么?我在stackoverflow上读过一些"类似的帖子",但它们对我没有帮助.(我使用GlassFish 4.0,JSF 2.2,JDK 1.7,Netbeans 7.3.1(Java EE pack)和Java EE 6.0)
<?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://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello from Facelets
<br/>
User: #{books.user.name}<br/>
1: #{param.pageId}<br/>
2: #{books.pageId}<br/>
<h:form>
<h:inputText value="#{user.name}" /><br/>
<h:inputText value="#{books.v1}" /><br/>
<h:inputText value="#{books.v2}" /><br/>
<h:inputText value="#{books.result}" /><br/>
<h:commandButton value="dodaj" action="#{books.add}" />
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
书
/*
* To change this template, choose Tools | Templates
* and open the template in the editor. …
Run Code Online (Sandbox Code Playgroud) 我正在将托管bean注入另一个托管bean的托管属性.
package com.books.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="book")
@RequestScoped
public class Book {
@ManagedProperty(value = "page")
private Page pages;
// Getter/setter
}
Run Code Online (Sandbox Code Playgroud)
package com.books.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="page")
@RequestScoped
public class Page {
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是,它抛出以下EL异常:
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/JSFTraining] threw exception [Unable to set property pages for managed bean book] with root cause
javax.el.ELException: Cannot convert page of type class java.lang.String to class com.books.beans.Page
at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:416)
at org.apache.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:46) …
Run Code Online (Sandbox Code Playgroud) jsf ×8
managed-bean ×6
cdi ×3
jsf-2 ×2
null ×2
constructor ×1
el ×1
facelets ×1
spring ×1
subclass ×1
viewparams ×1