这是我得到的:
<select id="box1">
<option>ABCDEFG</option>
</select>
<select id="box2">
<option>ABCDEFGHIJKLMNO</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我有2个不同的下拉列表.由于下拉列表的宽度取决于选项中最长文本的宽度,因此最终得到2个具有2个不同宽度的下拉列表.这使我的网页看起来很傻.
我想要的是设置它,以便我的两个下拉列表具有相同的宽度(我更喜欢宽度非常长,所以即使最长的项目也不会被截断).
我使用 Eclipselink 作为我的 JPA 提供程序,如何使字段自动递增?
我有这样的查询
SET @rownum := 0;
SELECT rank, id, point FROM
(
SELECT @rownum := @rownum + 1 AS rank, id, point FROM user ORDER BY point DESC
) AS result
WHERE id = 0;
Run Code Online (Sandbox Code Playgroud)
所以我EntityManager#createNativeQuery用来执行这个查询.
Object temp = em.createNativeQuery(sql).getSingleResult(); //sql is the above SQL query
Run Code Online (Sandbox Code Playgroud)
所以,现在的对象temp保存有关信息rank,id和point.请注意,这rank不是我的实体内部的属性,rank是在查询执行时计算的 - >不能将此对象强制转换为我的实体.
那么我怎样才能获得价值rank?
编辑
这是我正在寻找的答案.所以不要这样
Object temp = em.createNativeQuery(sql).getSingleResult();
Run Code Online (Sandbox Code Playgroud)
做这个
Object[] temp = (Object [])em.createNativeQuery(sql).getSingleResult();
Run Code Online (Sandbox Code Playgroud)
既然我想知道它的价值rank,那么我会这样做 …
我有一个java Enum类,在运行时我将从命令行读取一个值,我想将此值与我的Enum类中的值对应.Shipper是我的Enum课程.有没有更好的办法做到这一点这个,而不是if和else if下面?这看起来很难看.
private List<Shipper> shipperName = new ArrayList<Shipper>();
...
public void init(String s){
if(s.equals("a")){
shipperName.add(Shipper.A);
}else if(s.equals("b")){
shipperName.add(Shipper.B);
}else if(s.equals("c")){
shipperName.add(Shipper.C);
}else if(s.equals("d")){
shipperName.add(Shipper.D);
}else if(s.equals("e")){
shipperName.add(Shipper.E);
}else{
System.out.println("Error");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Shipper.class
public enum Shipper
{
A("a"),
B("b"),
C("c"),
D("e"),
F("f")
;
private String directoryName;
private Shipper(String directoryName)
{
this.directoryName = directoryName;
}
public String getDirectoryName()
{
return directoryName;
}
}
Run Code Online (Sandbox Code Playgroud) 所以我有这个反复执行的方法
public static boolean isReady(String dirPath, int numPdfInPrintJob){
File dir = new File(dirPath);
String[] fileList = dir.list(new FilenameFilter(){
public boolean accept(File file, String filename) {
return (filename.toLowerCase().endsWith(".pdf"));
}
});
if(fileList.length >= numPdfInPrintJob) return true;
else return false;
}
Run Code Online (Sandbox Code Playgroud)
使用anonymous class它的这个方法将创建FilenameFilter每次调用的新实例,并且我调用了很多这个方法.所以我想把它anonymous class变成一个singleton.所以我最初的想法是创建一个singleton看起来像这样的新类
public class PdfFileNameFilter implements FilenameFilter{
private PdfFileNameFilter(){} //non-instantible
//guarantee to only have one instance at all time
public static final PdfFileNameFilter INSTANCE = new PdfFileNameFilter();
public boolean accept(File dir, …Run Code Online (Sandbox Code Playgroud) 可能的重复:
如何使用 java 锁定文件(如果可能)
我有两个进程调用两个修改相同文本文件的 Java 程序。我注意到文本文件的内容缺少数据。我怀疑当一个 java 程序获取到文本文件的写入流时,我认为它会阻止另一个 java 程序修改它(就像当你打开一个文件时,你不能删除那个文件)。除了数据库,有没有办法解决这个问题?(不是说db解决方案不干净或不优雅,只是我们在操作这个文本文件时写了很多代码)
编辑
事实证明,我针对这个问题犯了一个错误。我的文本文件中的数据丢失的原因是,
ProcessA: 继续将数据行添加到文本文件中
ProcessB: 在开始时,将文本字段的所有行加载到一个List. 然后操作该列表的包含。最后,ProcessB将列表写回,替换文本文件的包含。
这在顺序过程中效果很好。但是当一起运行时,如果ProcessA向文件中添加数据,在ProcessB操作期间List,那么当ProcessB写List回时,无论ProcessA添加什么,都会被覆盖。所以我最初的想法是在ProcessB写List回之前,同步文本文件和List. 所以当我写List回时,它将包含所有内容。所以这是我的努力
public void synchronizeFile(){
try {
File file = new File("path/to/file/that/both/A/and/B/write/to");
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
FileLock lock = channel.lock(); //Lock the file. Block until release the lock
List<PackageLog> tempList = readAllLogs(file);
if(tempList.size() > …Run Code Online (Sandbox Code Playgroud) 我们可以同时将多个FileWriter流打开到同一文件吗?我写了一些代码来测试这一点,显然可以实现。这让我迷迷糊糊。由于如果我打开文件编写器来关闭文件,并且在关闭文件之前,我尝试删除该文件,所以我不能。那么,如何以及为什么一次可以打开多个FileWriter流到同一文件?这是我尝试的
private static final int SIZE = 1000;
public static void main(String[] args) throws IOException, InterruptedException {
File file = new File("C:\\dev\\harry\\data.txt");
FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (int i = 0; i < SIZE; i++) {
bufferedWriter.write("1\n");
Thread.sleep(100);
}
if (bufferedWriter != null) bufferedWriter.close();
if (fileWriter != null) fileWriter.close();
}
Run Code Online (Sandbox Code Playgroud)
我有另一个过程,做的事情完全相同,但是写2出来了,我在数据文件中得到了1和2。
我想使用java.nio.channels.FileChannel从文件中读取内容,但是我想像读取行一样BufferedReader#readLine()。我需要使用java.nio.channels.FileChannel而不是的java.io原因是,我需要在文件上放置一个锁,然后逐行读取该锁文件。所以我被迫使用java.nio.channels.FileChannel。请帮忙
编辑这是我的代码尝试使用FileInputStream获取FileChannel
public static void main(String[] args){
File file = new File("C:\\dev\\harry\\data.txt");
FileInputStream inputStream = null;
BufferedReader bufferedReader = null;
FileChannel channel = null;
FileLock lock = null;
try{
inputStream = new FileInputStream(file);
channel = inputStream.getChannel();
lock = channel.lock();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String data;
while((data = bufferedReader.readLine()) != null){
System.out.println(data);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
lock.release();
channel.close();
if(bufferedReader != null) bufferedReader.close();
if(inputStream != null) inputStream.close(); …Run Code Online (Sandbox Code Playgroud) (使用Glassfish 3.1的Java EE 6)
我有一个属性文件,我想在启动时只处理一次,所以我这样做了
public class Config implements ServletContextListener{
private static final String CONFIG_FILE_PATH = "C:\\dev\\harry\\core.cfg";
private static final String CONFIG_ATTRIBUTE_NAME = "config";
private long startupTime;
private ConfigRecord config;
@Override
public void contextInitialized(ServletContextEvent sce) {
this.startupTime = System.currentTimeMillis() / 1000;
this.config = new ConfigRecord(CONFIG_FILE_PATH); //Parse the property file
sce.getServletContext().setAttribute(CONFIG_ATTRIBUTE_NAME, this);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
//Nothing to do here
}
public ConfigRecord getConfig() {
return config;
}
public long getStartupTime() {
return startupTime;
}
}
Run Code Online (Sandbox Code Playgroud)
在web.xml …
首先,我想为我发布的长代码道歉,它们非常简单,它只是p:selectOneMenu重复17次(这就是为什么它很长).我遇到的问题是,如果我p:selectOneMenu在表单中有太多,那么selectOneMenu当用户点击它时,问题就会消失并且不会下降(除非我点击垃圾邮件selectOneMenu),列表不会下拉.奇怪的是,如果它只有1或2 selectOneMenu那么它工作正常(这就是我发布显示17下拉列表的代码的原因).这只发生在IE8中.这项工作很好的是IE6,7 FF,Chrome.
再一次:为长代码道歉
EDIT1:我只是编辑我的代码,foodList为我的托管bean 添加更多条目.这对于复制我的问题至关重要
<div id="MainWrapper">
<h:form id="myForm" styleClass="mainForm">
<h:panelGrid columns="2" columnClasses="columnStyle,columnStyle">
<h:panelGrid columns="3">
Select Food1:
<p:selectOneMenu id="food1" required="true" value="#{viewBean.selectedFood}"
styleClass="dropdown-width">
<f:selectItem itemLabel="Select One" itemValue=""/>
<f:selectItems value="#{viewBean.foodList}"/>
<p:ajax update=":myForm:errorFood1"/>
</p:selectOneMenu>
<p:message id="errorFood1" for="food1"/>
Select Food2:
<p:selectOneMenu id="food2" required="true" value="#{viewBean.selectedFood}"
styleClass="dropdown-width">
<f:selectItem itemLabel="Select One" itemValue=""/>
<f:selectItems value="#{viewBean.foodList}"/>
<p:ajax update=":myForm:errorFood2"/>
</p:selectOneMenu>
<p:message id="errorFood" for="food2"/>
Select Food3:
<p:selectOneMenu id="food3" required="true" value="#{viewBean.selectedFood}"
styleClass="dropdown-width">
<f:selectItem itemLabel="Select One" itemValue=""/>
<f:selectItems value="#{viewBean.foodList}"/> …Run Code Online (Sandbox Code Playgroud) java ×6
jpa ×2
jsf ×2
bytebuffer ×1
css ×1
eclipselink ×1
enums ×1
filechannel ×1
html ×1
io ×1
java-ee ×1
java-ee-6 ×1
javascript ×1
mysql ×1
nio ×1
primefaces ×1
refactoring ×1
servlets ×1
singleton ×1