我正在考虑将Java EE用于我的大学项目.以前,我使用C#作为桌面应用程序.我是Java和Java EE的新手.
我的问题是这个.在Java EE中启动项目之前我需要考虑什么?我正在考虑使用NetBeans作为我的IDE.这是一个好主意吗?我可以选择MS Sql Server或Oracle作为我的后端.
这个问题是关于Java EE 6,使用glassfish v3 embedded-all.
我有一个单元测试,使用EJBContainer来测试我的无状态EJB.问题是我在使用JNDI查找EJB(远程)时遇到问题:
setup() {
ctx = EJBContainer.createEJBContainer().getContext();
}
...
test() {
BookService bookService = (BookService)ctx.lookup("java:global/BookServiceEJB!com.something.service.BookService");
...
}
@Stateless
public class BookServiceEJB implements BookService {
...
}
@Remote
public interface BookService {
...
}
Run Code Online (Sandbox Code Playgroud)
给出例外:
javax.naming.NamingException: Lookup failed for 'java:global/BookServiceEJB!com.something.service.BookService' in SerialContext [Root exception is javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found]
...
caused by: javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found
Run Code Online (Sandbox Code Playgroud)
我尝试了几个JNDI资源路径:
例如
java:global/BookServiceEJB
java:global/BookService
Run Code Online (Sandbox Code Playgroud)
甚至:
java:global/BookShelf-1.0-SNAPSHOT/BookServiceEJB
Run Code Online (Sandbox Code Playgroud)
等等...
没有用
我没有配置任何xml部署文件,只有persistence.xmlMETA-INF.
测试使用maven surefire: …
当我使用命令按钮重定向到页面时,在我的项目中,你只需要给出没有扩展名的页面名称,然后在action属性中使用?faces-redirect = true,我将被重定向.
但是如果我想重定向到外部页面(例如:www.google.com)怎么办?
我试过很多方面:
www.google.com,google.com,http://google.com
但我失败了.
这就是我做的:
<h:form>
<h:commandButton
action="#{mainPageBB.goToLink}" value="#{msgs.clickhere}"/>
</h:form>
Run Code Online (Sandbox Code Playgroud)
然后是支持bean:
@Named("mainPageBB")
@RequestScoped
public class MainPageBB {
@EJB
private ILinkManagerEJB linkManagerEJB;
public String goToLink() {
String link = linkManagerEJB.retrieveLink();
if(link != null) {
System.out.println(link);
return link.trim() + "?faces-redirect=true";
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
注意:retrieveLink()返回的值; 始终是www.google.com(100%肯定)
我在控制台中完全没有错误,页面只是刷新.此外,我确信第一个if子句验证为true,所以我认为没有理由跳转到返回null.
更新
我尝试使用外部上下文,但是我得到了404,因为它将当前url附加到链接字符串:
public String goToRandomLink() {
String link = linkManagerEJB.retrieveRandomLink();
if(link != null) {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
try {
externalContext.redirect(link.trim());
} catch (IOException e) { …Run Code Online (Sandbox Code Playgroud) 我是JavaEE 6的新手,我了解到它支持名为AsyncServlets的异步servlet.所以我尝试使用JavaEE6创建一个小程序.
这是我的servlet代码
@WebServlet(name = "AsyncServlet", urlPatterns = {"/AsyncServlet"}, asyncSupported=true)
public class AsyncServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AsyncContext aCtx = request.startAsync(request, response);
Executor executor = Executors.newSingleThreadExecutor();
executor.execute(new MyClass(aCtx));
System.out.println("Original thread is freed");
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行servlet时,我得到一个异常java.lang.IllegalStateException:不支持.我正在使用tomcat 7.0.14作为服务器.我没有创建web.xml.这段代码中的问题在哪里?
编辑:
这是异常的完整堆栈跟踪
SEVERE: Servlet.service() for servlet [AsyncServlet] in context with path [/AsyncTest] threw exception
java.lang.IllegalStateException: Not supported.
at org.apache.catalina.connector.Request.startAsync(Request.java:1618)
at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1031)
at javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:379)
at com.icbt.demo.servlet.AsyncServlet.doGet(AsyncServlet.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个通用 DAO 以在我的项目中使用。到目前为止,我已经实现的代码如下:
基础DAO
public abstract class BaseDAO<T> {
private Class<T> entityClass;
public BaseDAO(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public T persist(T entity) {
getEntityManager().persist(entity);
return entity;
}
public T edit(T entity) {
getEntityManager().merge(entity);
return entity;
}
public T remove(T entity) {
getEntityManager().remove(entity);
return entity;
}
public List<T> findAll() {
Query q = getEntityManager().createQuery("SELECT e FROM " + entityClass.getName()
+ " e");
List<T> list = (List<T>) q.getResultList();
return list;
}
public T find(Long id) { …Run Code Online (Sandbox Code Playgroud) 当我尝试访问像writerPage这样的受保护页面时,Shiro会继续将我重定向到登录页面

这是shiro.ini文件
[users]
admin = p
mike = p, reader, writer
joe = p, writer
[urls]
/success/** = authc
/writer/** = authc
Run Code Online (Sandbox Code Playgroud)
并且服务器显示一些错误:
INFO: PWC1412: WebModule[/ShiroTest3_mvn] ServletContext.log():Cleaning up Shiro Environment
INFO: PWC1412: WebModule[/ShiroTest3_mvn] ServletContext.log():Initializing Shiro environment
GRAVE: 105 [http-thread-pool-4848-(1)] INFO org.apache.shiro.web.env.EnvironmentLoader - Starting Shiro environment initialization.
GRAVE: 997 [http-thread-pool-4848-(1)] INFO org.apache.shiro.web.env.EnvironmentLoader - Shiro environment initialized in 882 ms.
INFO: Loading application com.mycompany_ShiroTest3_mvn_war_1.0-SNAPSHOT at /ShiroTest3_mvn
INFO: com.mycompany_ShiroTest3_mvn_war_1.0-SNAPSHOT was successfully deployed in 1 606 milliseconds.
Run Code Online (Sandbox Code Playgroud)
这是web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" …Run Code Online (Sandbox Code Playgroud) 有没有办法禁用嵌入式Servlet过滤器?
我的项目有一个依赖jar,它包含(在jar中)@WebFilter映射到的"/*".
我需要的jar(它有很多公共类我公司的),但这个新项目并不需要这个网页过滤,实际上这个新项目将无法工作,因为该过滤器检查用户身份验证和新的项目没有"loggedUser".这就像一个网站
谢谢
当我们将持久化EJBTimer与@schedule和persistent = true一起使用时,将其部署到集群,然后我们在@Schedule中更改实际计划并重新部署到集群,原始计划是否被新计划替换(删除并添加了新参数),或者两个时间表都保持活动状态(请记住设置了persistent = true)
这是我到目前为止所读到的内容 - Each scheduler instance has a unique jndi name and @schedule automatically creates a timer through application deployment so it would be better to remove the automatic created EJBTimer or cancel the original schedule to avoid trouble. 但我不知道如何以编程方式取消原始计划,或者如果原始和更改的计划都保持活动状态,则需要由websphere管理员完成
同样从本文档中,removeAutomaticEJBTimers命令用于从指定的调度程序中删除计时器,但这似乎也出现在websphere管理员的区域,而不是开发人员.
开发人员如何以编程方式取消使用@Schedule注释创建的自动EJBTimer?
我正在使用Java EE 6和Websphere 8.5和EJB 3.1.
我有两个使用maven 3在WAR文件中打包的eclipse Java EE 6项目.现在,他们应该在第三个项目中共享JPA实体,因为它们都使用相同的数据库.
在对我的问题进行研究时,我发现了一些提示,例如在persistence.xml中引用了一个常见的jar,但我没有成功使其工作.特别问:
1)包含公共实体的项目是否具有persistence.xml文件?如果是这样,它与其他项目的不同之处是什么?
2)两个项目中引用的常用实体到底有多准确?我是否在persistence.xml中使用该标记作为参考?如果是这样,如果MyCommin.jar位于WEB-INF/lib中,"lib/MyCommon.jar"是否是正确的使用方式?
3)如果将应用程序部署为JBoss 6中的WAR或爆炸存档,它会有什么不同吗?
4)当通过添加到服务器运行时和发布时在eclipse中部署时,与部署外部eclipse有什么不同吗?
5)所描述的方式是将公共实体放在一个单独的项目中,创建一个JAR并在另一个项目中使用该JAR是一种合理的方法来处理共享公共实体类的问题还是有更好的方法?
我有一个基于泛型的特定事件处理的想法,但似乎Weld无法处理它们.我问谷歌但是找不到替代的CDI扩展.
问题:是否有CDI扩展,可以处理泛型类型事件的事件传播?
在下面我有明确的问题.
我有三个常规事件,EntityCreated,EntityChanged和EntityDeleted.它们的基类定义如下:
public abstract class DatabaseEvent<TargetType> {
public TargetType target;
public DatabaseEvent(TargetType target) {
this.target = target;
}
}
Run Code Online (Sandbox Code Playgroud)
然后事件是简单的继承类:
public class EntityCreatedEvent<TargetType> extends DatabaseEvent<TargetType> {
public EntityCreatedEvent(TargetType target) {
super(target);
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样解雇他们:
public abstract class MyHome<EntityType> {
private EntityType entity;
@Inject
Event<EntityCreatedEvent<EntityType>> entityCreatedEvent;
public void fireCreatedEvent() {
EntityCreatedEvent<EntityType> payload = new EntityCreatedEvent<EntityType>(entity);
entityCreatedEvent.fire(payload);
}
}
Run Code Online (Sandbox Code Playgroud)
我想像这样观察它们:
public void handleProjectCreated(@Observes EntityCreatedEvent<Project> event) { ... }
Run Code Online (Sandbox Code Playgroud)
启动服务器时,Weld告诉我它无法处理泛型类型的事件.CDI的做法是使用额外的限定符而不是泛型来消除它们,例如:
public void handleProjectCreated(@Observes @ProjectEvent EntityCreatedEvent event) { ... }
Run Code Online (Sandbox Code Playgroud)
但是,我从该 …