在"创建新用户"jsf页面中,我有一个带有自定义转换器的SelectOneMenu和一个noSelectionOption selectItem,如下所示:(无关代码无关)
NewUser.xhtml
<h:form>
<h:selectOneMenu value="#{newUserController.user.department}"
required="true" converter="departmentConverter">
<f:selectItem itemLabel="Select a department" noSelectionOption="true"/>
<f:selectItems value="#{newUserController.departments}"
var="dep" itemLabel="#{dep.name}" itemValue="#{dep}"/>
</h:selectOneMenu>
<p:commandButton action="#{newUserController.saveUser}"
value="#{bundle.Save}"
ajax="false"/>
</h:form>
Run Code Online (Sandbox Code Playgroud)
NewUserController.java
@ManagedBean
@ViewScoped
public class NewUserController implements Serializable {
private static final long serialVersionUID = 10L;
@EJB private UserBean userBean;
private List<Department> departments;
private User user;
public NewUserController () {
}
@PostConstruct
public void init(){
user = new User();
departments = userBean.findAllDepartments();
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user …Run Code Online (Sandbox Code Playgroud) 我是新手,我正在尝试使用glassfish在Web应用程序中实现它.我已经使用JDBC实现了自己的loghandler,但是我应该如何配置系统来使用它呢?Glassfish自己的日志记录让我感到困惑,我应该在domain文件夹中的logging.properties中将我自己的配置与glassfish的配置混合,还是以某种方式可能只有我自己设置的第二个logging.properties?我尝试将我的处理程序添加到glassfish配置中的根记录器,如下所示:
的glassfish /域/域1 /配置/ logging.properties:
handlers=com.company.myapp.util.JDBCLogHandler, java.util.logging.ConsoleHandler
Run Code Online (Sandbox Code Playgroud)
但是,我的记录器
private static final Logger logger = Logger.getLogger(MyClass.class.getName());
Run Code Online (Sandbox Code Playgroud)
处理程序列表为空.如何在不以编程方式执行的情况下添加处理程序?我没有包含loghandler的代码,因为当我以编程方式添加它时它确实有用.
以某种方式可以使用JPA 2 Criteria API执行以下查询之一吗?基本上,Profiles使用了许多模板.系统中的每个用户都有任意数量的配置文件和两个活动配置文件.我想获取所有模板以及拥有使用它的活动配置文件的用户数.
备选方案1
SELECT t.id, count(p.id)
FROM template t
LEFT JOIN profile p ON p.template_id = t.id
LEFT JOIN users u ON (u.active_profile_id = p.id OR u.active_personal_profile_id = p.id)
GROUP BY t.id
Run Code Online (Sandbox Code Playgroud)
备选方案2
SELECT t.id, count(p.id)
FROM COMPETENCE.template t
LEFT JOIN COMPETENCE.profile p ON (p.template_id = t.id)
LEFT JOIN users u1 on u1.active_profile_id = p.id
LEFT JOIN users u2 on u2.active_personal_profile_id = p.id
GROUP BY t.id
Run Code Online (Sandbox Code Playgroud)
如果我跳过了获取所有模板的要求,我可以简单地使用User和Profile作为Roots,以下就足够了:
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<User> from = cq.from(User.class);
Root<Profile> from2 = …Run Code Online (Sandbox Code Playgroud) 鉴于以下情况:
@Entity
public class A {
@OneToMany(mappedBy = "a", cascade = CascadeType.ALL)
private List<B> bList;
}
@Entity
public class B {
@ManyToOne()
@JoinColumn(name = "a_id", referencedColumnName = "id")
private A a;
@ManyToOne()
@JoinColumn(name = "c_id", referencedColumnName = "id")
private C c;
}
@Entity
public class C {
@OneToMany(mappedBy="c", cascade=CascadeType.ALL, orphanRemoval=true)
@CascadeOnDelete // eclipselink specific optimization annotation
private List<B> bList;
}
Run Code Online (Sandbox Code Playgroud)
换句话说:对象A和对象C都包含许多B对象.
当我删除一个C对象时(从技术上讲,我正在更新一个包含多个C对象并使用orphanremoval的对象),我希望删除所有引用的B对象,它们按照预期的方式使用当前注释.但是,实体管理器似乎并不理解位于其缓存中的对象A现在已经丢失了一些孩子.如果我有一个A的实例,我当然必须手动更新它的bList,或者做一个新的查询来更新它,但即使是新获取的A对象仍然过时.重申:
怎么解决这个问题?我希望实体管理器能够自动更新其持久化上下文,或者在@JoinColumn上提供级联注释,但在这里似乎都不是这种情况.
编辑:似乎问题在于对象C的bList在对象A的bList更新时没有得到更新(因此不能级联更改).我不知道为什么虽然..仍然注意到我在谈论持久性上下文而不是实例化对象.
是否真的应该对使用render ="false"的元素进行EL解析?这导致我很多空指针异常和类似的问题.看下面的例子:
<p:tab title="#{userCompetenceController.getTreeName(3)}" rendered="#{!empty userCompetenceController.getTreeName(3)}">
<xdin:competenceTable id="competenceBox3"
profile="#{userCompetenceController.selectedProfile}"
tree="#{userCompetenceController.getCompetenceTree(3)}"
maxHeight="500px"/>
</p:tab>
Run Code Online (Sandbox Code Playgroud)
主要问题(除了性能)是xdin:competenceTable不支持null tree-attribute.getTreeName(int index)在这种情况下返回null,然后调用getCompetenceTree(3)返回null,即使它的parent(p:tab)有rendered="false"
简而言之:xdin:competenceTable即使它是父级的,也会被EL解析rendered="false".为什么?
我正在尝试在登录后实现重定向,这意味着我不能再使用glassfish内置的表单身份验证设置来自动处理这些事情.首先,我需要在请求受保护页面时控制重定向到登录页面.据我了解,这是通过过滤器完成的.这种方法可以与web-xml中的安全约束相结合吗?实际上,我的过滤器根本没有被调用,因为glassfish只是接管并向用户抛出一个基本的登录框,并且即使没有设置登录配置也会忽略所有过滤器.基本上,在glassfish中配置安全性约束时,我没有设法在用户登录之前调用过滤器.
我是否真的需要在过滤器中手动接管安全性才能使其正常工作?如果是这样的话,实施似乎很糟糕.
使用带有JSF 2的glassfish 3.1和使用request.login手动登录的自定义登录页面.
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">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value><!--Production-->Development</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>de.odysseus.el.ExpressionFactoryImpl</param-value>
</context-param>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>*.jsf</url-pattern>
</servlet-mapping>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.xdin.competence.jsf.util.LoginFilter</filter-class>
</filter>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
<!--<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/viewExpired.jsf</location>
</error-page>-->
<security-constraint>
<display-name>ManagerArea</display-name>
<web-resource-collection>
<web-resource-name>ManagerArea</web-resource-name>
<description/>
<url-pattern>/manager/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>Manager-role</role-name>
<role-name>Admin-role</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<display-name>EmployeeArea</display-name>
<web-resource-collection>
<web-resource-name>EmployeeConstraint</web-resource-name>
<description/>
<url-pattern>/user/Overview.jsf</url-pattern>
<url-pattern>/user/PrepareReport.jsf</url-pattern>
<url-pattern>/user/Search.jsf</url-pattern>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>Employee-role</role-name>
<role-name>Admin-role</role-name>
<role-name>Manager-role</role-name>
<role-name>OKIF-role</role-name> …Run Code Online (Sandbox Code Playgroud) 简化,我的错误处理程序如下所示:
@Override
public void handle() throws FacesException {
Iterator<ExceptionQueuedEvent> unhandledExceptionQueuedEvents = getUnhandledExceptionQueuedEvents().iterator();
FacesContext context = FacesContext.getCurrentInstance();
if (unhandledExceptionQueuedEvents.hasNext()) {
Throwable exception = unhandledExceptionQueuedEvents.next().getContext().getException();
unhandledExceptionQueuedEvents.remove();
context.getExternalContext().dispatch("/error.jsf");
}
while (unhandledExceptionQueuedEvents.hasNext()) {
unhandledExceptionQueuedEvents.next();
unhandledExceptionQueuedEvents.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
简化的template.xhtml
<html>
<f:view>
<h:head>
<!-- some stuff here -->
</h:head>
<h:body>
<div id="menu"><!-- menu content --></div>
<div id="content">
<ui:insert name="body"></ui:insert>
</div>
</h:body>
</f:view>
</html>
Run Code Online (Sandbox Code Playgroud)
error.xhtml
<html>
<ui:composition template="/template.xhtml">
<ui:define name="body">
ERROR
</ui:define>
</ui:composition>
</html>
Run Code Online (Sandbox Code Playgroud)
但是当渲染error.jsf时,使用ui:composition进行模板化是非常错误的.JSF将template.xhtml渲染到它周围,然后再开始渲染模板.结果是一个页面,菜单呈现两次,所有资源再次包含在页面中间.它的开头看起来像这样:
<div id="menu></div>
<div<?xml version="1.0" encoding="UTF-8" ?="">
<!-- Page included once more --> …Run Code Online (Sandbox Code Playgroud) 我正在尝试对包含ThreadLocal的类进行单元测试,并希望通过在每个测试中启动一个新线程来使测试不会相互影响.但是,他们仍然这样做,我不明白为什么.
@Test
public void testThread() {
System.out.println(Thread.currentThread().getId());
new Thread(){
@Override
public void run(){
System.out.println(Thread.currentThread().getId());
}
}.run();
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
1
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么即使启动新线程,ID也是一样的吗?
jsf-2 ×4
glassfish ×2
jpa-2.0 ×2
concurrency ×1
converter ×1
criteria-api ×1
eclipselink ×1
el ×1
java ×1
java-ee-6 ×1
jsf ×1
logging ×1
login ×1
security ×1