当我尝试在托管bean中验证来自我的JSF的用户输入时,我遇到了一些问题.我在控制台中收到了验证消息,但我没有在页面中看到它.我不明白问题出在哪里.
这是控制台输出:
信息:内部验证方法!! 信息:没有比赛!信息:实例化org.hibernate.validator.engine.resolver.JPATraversableResolver的实例.信息:警告:FacesMessage已入队,但可能尚未显示.的SourceID = bRegForm:名称[严重性=(INFO 0),摘要=(您的名字不能包含特殊字符),细节=(您的名字不能包含特殊字符)]
这就是我制作JSF输入组件的方式:
<h:inputText id="name" value="#{registrationController.name}" validator="#{registrationController.validateName}" required="true">
<h:message for="name"/>
</h:inputText>
Run Code Online (Sandbox Code Playgroud)
这就是我如何创建验证方法:
@ManagedBean
@RequestScoped
public class RegistrationController {
...
public void validateName(FacesContext context, UIComponent validate,
Object value) {
String inputFromField = (String) value;
String simpleTextPatternText = "^[a-zA-Z0-9]+$";
Pattern textPattern = null;
Matcher nameMatcher = null;
textPattern = Pattern.compile(simpleTextPatternText);
nameMatcher = textPattern.matcher(inputFromField);
System.out.println("Inside validation method!!");
if (!nameMatcher.matches()) {
System.out.println("NO MATCH!!!");
FacesMessage msg = new FacesMessage(
Your name cannot contain special characters);
throw new ValidatorException(msg);
}
} …Run Code Online (Sandbox Code Playgroud) 可能重复:
为什么类实现Serializable接口?
我正在使用这里的教程:http://www.objectdb.com/tutorial/jpa/eclipse/ee/entity
我想知道为什么这堂课会延伸Serializable?我已经阅读了这个课程的描述,我不明白serialVersionUID它的重要性以及为什么它对我的模型是必要的.
我有一个注释:
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Example {
}
Run Code Online (Sandbox Code Playgroud)
以及用于处理的拦截器类:
@Interceptor
@Example
public class ExampleInterceptor implements Serializable {
...
}
Run Code Online (Sandbox Code Playgroud)
我想添加一个参数文本:
public @interface Example {
String text();
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何处理拦截器类中的参数.如何修改类的注释?
@Interceptor
@Example(text=???????)
public class ExampleInterceptor implements Serializable {
...
}
Run Code Online (Sandbox Code Playgroud)
如果我写@Example(text="my text"),则只在方法/类注释时调用拦截器@Example(text="my text").但是我希望在参数值上独立调用拦截器 - @Example(text="other text").
以及如何获取参数值?我是否必须使用反思或有更好的方法吗?
假设我有一个定义两个视图的EJB:
两个接口共享相同的方法签名,因此它类似于:
public interface MyBusinessCommon {
void myMethod(Object o);
}
@Local
public interface MyBusinessLocal extends MyBusinessCommon { }
@Remote
public interface MyBusinessRemote extends MyBusinessCommon { }
@Stateless
public class MyBusinessBean implements MyBusinessLocal, MyBusinessRemote {
public void myMethod(Object o) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法弄清楚EJB本身(或其拦截器?)内部调用的EJB视图?
假设我想根据使用的视图执行不同的授权程序.远程应该更加受限制,本地不应该.
我可以调用SessionContext#getInvokedBusinessInterface()但这只给了我关于类对象的信息 - 而不是关于它的EJB语义.明确地使用反射来检查接口或bean上的注释是否存在是不够的(在ejb-jar.xml?中定义的视图怎么样?)
我怀疑是否有可能使用直接的EJB规范,但也许我错过了一些东西.
如果没有,是否可以从应用程序服务器的内部获取此信息?(我们只考虑JBoss AS 7.x,Glassfish 3.x和TomEE 1.5.1).
我非常了解Maven,但我通常使用Netbeans,这使得部署Java EE应用程序非常容易 - 并处理任何更改的热部署.
我现在使用的团队使用Eclipse作为他们选择的IDE,但他们之前都没有使用过Maven项目.所以我需要知道如何正确地将Eclipse的项目方面添加到我们拥有的maven项目中,以便通过WTP支持(热)部署.我们正在使用Weblogic 12c进行这项特殊的努力.
我们的项目布局非常简单:
super-project (pom)
project-ear (pom)
project-ejb (pom)
project-web (pom)
Run Code Online (Sandbox Code Playgroud)
感谢您指点我的任何资源.
有没有办法创建一个拦截器限定符注释,忽略注释字符串值以进行限定?
例如:
Log.java
@Inherited
@InterceptorBinding
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
String value() default ""; // <---- ignore this
}
Run Code Online (Sandbox Code Playgroud)
LogInterceptor.java
@Log
@Interceptor
public class LogInterceptor implements Serializable {
...
}
Run Code Online (Sandbox Code Playgroud)
Usage.java
@Log("message for this log")
public String interceptedMethod(String param) {
...
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为注释value("message for this log")作为限定符,但我想使用value()not作为限定符,但使用消息日志.
我有一个@AroundInvoke REST Web服务拦截器,我想用它来记录常见数据,如类和方法,远程IP地址和响应时间.
使用InvocationContext获取类和方法名称很简单,只要被拦截的Rest服务在其参数列表中包含@Context HttpServletRequest,就可以通过HttpServletRequest获得远程IP.
但是有些REST方法的参数中没有HttpServletRequest,在这些情况下我无法弄清楚如何获取HttpServletRequest对象.
例如,以下REST Web服务没有@Context HttpServletRequest参数
@Inject
@Default
private MemberManager memberManager;
@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Member add(NewMember member) throws MemberInvalidException {
return memberManager.add(member);
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试将它直接注入我的拦截器,但是(在JBoss 6.1上)它总是为空...
public class RestLoggedInterceptorImpl implements Serializable {
@Context
HttpServletRequest req;
@AroundInvoke
public Object aroundInvoke(InvocationContext ic) throws Exception {
logger.info(req.getRemoteAddr()); // <- this throws NPE as req is always null
...
return ic.proceed();
Run Code Online (Sandbox Code Playgroud)
我想建议一种可靠的方法来访问HttpServletRequest对象 - 甚至只是Http Headers ...无论REST服务是否包含参数.
任何人都可以解释在Java EE 6应用程序中实现Singleton的完整过程吗?我假设我不应该以声明静态变量的典型方式创建单例并且应该使用@Singleton注释?我必须这样做吗?
这仅仅是一个声明它的情况@Singleton,就是这样吗?我还要上课了吗?
那么我需要做什么才能访问其他类中的单例?
我在@Resource注释的两个属性之间感到困惑.
Java Documentations说:
mappedName:此资源应映射到的产品特定名称.由name元素或defaultaulted定义的此资源的名称是使用资源的应用程序组件的本地名称.(它是JNDI java:comp/env命名空间中的名称.)许多应用程序服务器提供了将这些本地名称映射到应用程序服务器已知资源名称的方法.此映射名称通常是全局JNDI名称,但可以是任何表单的名称.
lookup:引用指向的资源的名称.它可以使用全局JNDI名称链接到任何兼容的资源.
我的问题是如何根据什么标准在mappedName和lookup之间进行选择?
我创建了一个自定义注释,如下所示
@InterceptorBinding
@Retention(RUNTIME)
@Target(TYPE, METHOD)
public @interface Traceable {}
Run Code Online (Sandbox Code Playgroud)
我写了一个拦截器,如下所示
@Traceable
@Interceptor
public class EnterExitLogger {
@AroundInvoke
public Object aroundInvoke(InvocatiobContext c) {}
}
Run Code Online (Sandbox Code Playgroud)
拦截器和注释位于名为common-utils的模块中.
我在类级别用@Traceable注释了我的目标类,如下所示
@Traceable
public class CDIManagedBean {
}
Run Code Online (Sandbox Code Playgroud)
我在beans.xml文件中声明了拦截器条目,如下所示
<interceptors>
<class>my.package.EnterExitLogger</class>
</interceptors>
Run Code Online (Sandbox Code Playgroud)
目标类位于单独的模块中.beans.xml位于目标类模块的META-INF目录中.
目标类的方法是从rest类调用的.当我调用方法时,不会调用拦截器的AroundInvoke方法.
我阅读了文档,并了解拦截器应该包含一个公共的无参数构造函数.我加了.但仍然没有调用拦截器.
我在阅读完文档后在自定义注释中添加了@Inherited.但仍然没有调用拦截器.
从文档中我注意到拦截器实现了Serializable接口.虽然没有提到我也实现了Serializable.仍然没有奏效.
然后我从拦截器,beans.xml文件和目标类中删除了自定义注释.我还从拦截器中删除了public no参数构造函数并删除了Serializable.
然后我用目标类注释@Interceptors(EnterExitLogger.class)并调用了流程.我的拦截器被调用了.
任何人都可以告诉我如何处理InterceptorBinding?
PS
我正在WAS 8.5服务器上部署我的耳朵.
java-ee-6 ×10
java ×5
java-ee ×5
interceptor ×4
ejb-3.1 ×2
jboss-weld ×2
annotations ×1
cdi ×1
eclipse ×1
ejb ×1
ejb-jar.xml ×1
jax-rs ×1
jboss ×1
jndi ×1
jsf ×1
jsf-2 ×1
maven ×1
qualifiers ×1
singleton ×1
websphere-8 ×1