在我的应用程序中,我连接到服务器以验证用户.这是代码:
try {
Properties prop = new Properties();
prop.put("mail.smtp.starttls.enable","true");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.connectiontimeout", 1000);
Session session = Session.getInstance(prop, null);
Transport transport = session.getTransport("smtp");
transport.connect("mion.elka.pw.edu.pl", 587, registerLog, registerPass);
transport.close();
return true;
} catch (NoSuchProviderException ex) {
Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch(AuthenticationFailedException ex) {
Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (MessagingException ex) {
Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
Run Code Online (Sandbox Code Playgroud)
我将连接超时设置为1000毫秒= 1秒,但它被忽略.当我调试并设置错误的用户名和密码时,我抓住了
javax.mail.MessagingException: java.net.SocketTimeoutException: Read timed out
Run Code Online (Sandbox Code Playgroud)
不是在1000毫秒之后,而是在5000*60毫秒= 5分钟之后
怎么了 ?我怎样才能减少时间?
如果我有使用Hibernate的方法,像这样:
public <T> T typedQuery(Query q, Class<T> type) {
List<T> results = q.list();
//result will be null or empty List ?
}
Run Code Online (Sandbox Code Playgroud)
如果查询不会从表中获取任何记录,那么result
会是null
空列表吗?
我有简单的测试网络应用程序:第一页 - index.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<html>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
First page
<h:form>
<h:commandButton value="Go to Next page" action="next"/>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
和下一页 - next.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<html>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Next page
<h:form>
<h:commandButton value="Go to First page" action="index"/>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,我在浏览器上有该URL:
http://localhost:8080/Test/
Run Code Online (Sandbox Code Playgroud)
和index.xhtml作为第一页.
然后当我点击"转到下一页"我有网址
http://localhost:8080/Test/index.xhtml
Run Code Online (Sandbox Code Playgroud)
和next.xhtml页面.当我点击"转到第一页"页面更改(到index.xhtml)但是url它
http://localhost:8080/Test/next.xhtml
Run Code Online (Sandbox Code Playgroud)
web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<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>*.xhtml</url-pattern>
</servlet-mapping> …
Run Code Online (Sandbox Code Playgroud) 在我的Web应用程序中,我在Apache Tomcat(TomEE)/7.0.37服务器上使用OpenJPA.我使用Netbeans自动生成类("实体类来自数据库......"和"会话Bean来自实体类......").在SessionBean(例如UserFacade)我想获得EntityManager:
@Stateless
public class UserFacade extends AbstractFacade<User> {
@PersistenceContext(unitName = "CollDocPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我通过上面的方式得到它时,我得到了null.当我通过:
@Override
protected EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("CollDocPU");
EntityManager ecm = emf.createEntityManager();
return ecm;
}
Run Code Online (Sandbox Code Playgroud)
ecm不是null,没关系
我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="CollDocPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>model.entity.StudentAddSolution</class>
<class>model.entity.Lecturer</class>
<class>model.entity.Solution</class>
<class>model.entity.Student</class>
<class>model.entity.Course</class>
<class>model.entity.File</class>
<class>model.entity.CourseHasLecturer</class>
<class>model.entity.Mail</class>
<class>model.entity.StudentAtCourse</class>
<class>model.entity.Roles</class>
<class>model.entity.Task</class>
<class>model.entity.User</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:11080/myBase?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="pass,"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property …
Run Code Online (Sandbox Code Playgroud) 我找到了一些答案:https://stackoverflow.com/a/21218921/2754014关于依赖注入.没有任何注释@Autowired
,@Inject
或,或@Resource
.让我们假设这个示例TwoInjectionStyles
bean 没有任何XML配置(简单除外)<context:component-scan base-package="com.example" />
.
在没有指定注释的情况下注入是否正确?
如果我在我的jsf页面上有这个代码:
<c:choose>
<c:when test="${empty example1}">
</c:when>
<c:when test="${empty example2}">
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
Run Code Online (Sandbox Code Playgroud)
它将像java switch
语句一样工作case
和break
- 如果第一次是真的,第二次不会测试,对吧?
我应该写什么来获得" switch
声明case
但没有break
":
当第一个C:when
是真的东西添加到页面,当第二个是真时,某些东西也会添加到页面
我收集List<Foo>
的Foo
要素:
class Foo {
private TypeEnum type;
private int amount;
//getters, setters ...
}
Run Code Online (Sandbox Code Playgroud)
Foo
类型可以TypeEnum.A
和TypeEnum.B
.
我想只Foo
从列表中获取那些元素type == TypeEnum.B
然后amount
大于零(amount > 0
)的元素.
我怎么能用Java 8 Streams filter()
方法做到这一点?
如果我使用:
List<Foo> l = list.stream()
.filter(i -> i.getType().equals(TypeEnum.B) && i.getAmount() > 0)
.collect(Collectors.<Foo>toList());
Run Code Online (Sandbox Code Playgroud)
我得到了Foo
元素,TypeEnum.B
但没有TypeEnum.A
.
我的h有问题:commandButton"登录":当我使用@ViewScoped并按下此按钮时会出现ViewExpiredException,但是当我使用@SessionScoped时,没有任何错误.
堆栈跟踪:
javax.faces.application.ViewExpiredException: /pages/register.xhtmlNo saved view state could be found for the view identifier: /pages/register.xhtml
at org.apache.myfaces.lifecycle.RestoreViewExecutor.execute(RestoreViewExecutor.java:132)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Run Code Online (Sandbox Code Playgroud)
我的页面:
<h:form>
<h:panelGrid columns="2" >
<h:outputLabel value="Login:"/>
<h:inputText value="#{registerController.registerLog}"/>
<h:outputLabel value="#{msg.password}"/>
<h:inputSecret id="pass" …
Run Code Online (Sandbox Code Playgroud) 我在理解现有代码时遇到问题。我想知道 Java 如何管理抛出异常并以相同的方法捕获它。我在其他问题中找不到它,所以我准备了一个例子。在 Java 中运行下面的代码会输出什么?
public static void main(String [ ] args) {
try{
System.out.println("1");
method();
}
catch(IOException e) {
System.out.println("4");
}
}
public static void method() throws IOException {
try {
System.out.println("2");
throw new IOException();
}
catch(IOException e) {
System.out.println("3");
}
}
Run Code Online (Sandbox Code Playgroud)
它会是1 2 3还是1 2 4?
在我的Web应用程序中,我在Apache Tomcat(TomEE)/7.0.37服务器上使用OpenJPA.
我的实体User.class:
@Entity
@Table(name = "USER")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Basic(optional = false)
@Column(name = "id_user")
private Integer idUser;
@Size(max = 8)
@Column(name = "login")
private String login;
@Size(max = 128)
@Column(name = "password")
private String password;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
private Lecturer lecturer;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
private Student student;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<UserHasRoles> userHasRolesList;
//constructors, getters, setters
} …
Run Code Online (Sandbox Code Playgroud) 我想通过我的Web应用程序插入MySQL数据库信息.我是在TomEE服务器上通过OpenJPA来做的,例如:
public void addLecturer(String name, String surname) {
Lecturer lect = new Lecturer(name,surname);
em.persist(lect);
}
Run Code Online (Sandbox Code Playgroud)
Lecturer是实体类.问题是应用程序以latin1编码插入数据.我想以UTF-8编码插入它.
当我在命令行终端中通过简单的MySQL命令插入数据时,我获得UTF-8编码的信息.
我看了这个,我看到character_set_server被设置为latin1,但我不知道这是问题还是如何改变它:
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
Run Code Online (Sandbox Code Playgroud)
编辑
我有同样的问题:
和
java ×7
jsf ×3
jpa ×2
jsf-2 ×2
openjpa ×2
tomee ×2
annotations ×1
autowired ×1
break ×1
encoding ×1
exception ×1
hibernate ×1
jakarta-mail ×1
java-8 ×1
java-stream ×1
jpql ×1
jstl ×1
list ×1
loops ×1
myfaces ×1
mysql ×1
navigation ×1
null ×1
persistence ×1
spring ×1
throw ×1
try-catch ×1
url ×1
view-scope ×1