小编Tha*_*ham的帖子

有没有办法修复下拉列表的宽度?

这是我得到的:

<select id="box1">
   <option>ABCDEFG</option>
</select>  

<select id="box2">
   <option>ABCDEFGHIJKLMNO</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我有2个不同的下拉列表.由于下拉列表的宽度取决于选项中最长文本的宽度,因此最终得到2个具有2个不同宽度的下拉列表.这使我的网页看起来很傻.

我想要的是设置它,以便我的两个下拉列表具有相同的宽度(我更喜欢宽度非常长,所以即使最长的项目也不会被截断).

html javascript drop-down-menu

5
推荐指数
2
解决办法
1万
查看次数

JPA:如何使字段自动递增

我使用 Eclipselink 作为我的 JPA 提供程序,如何使字段自动递增?

mysql jpa auto-increment eclipselink

5
推荐指数
1
解决办法
1万
查看次数

JPA:createNativeQuery.getSingleResult()返回一个对象,如何在该对象中获取一个属性的值

我有这样的查询

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,idpoint.请注意,这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 jpa

5
推荐指数
1
解决办法
2万
查看次数

Java枚举和if语句,有更好的方法吗?

我有一个java Enum类,在运行时我将从命令行读取一个值,我想将此值与我的Enum类中的值对应.Shipper是我的Enum课程.有没有更好的办法做到这一点这个,而不是ifelse 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)

java enums

5
推荐指数
2
解决办法
1万
查看次数

通过使用singleton替换匿名类来减少内存占用量.但需要更多地重构设计

所以我有这个反复执行的方法

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 singleton refactoring

5
推荐指数
1
解决办法
431
查看次数

Java:如何处理试图修改同一个文件的两个进程

可能的重复:
如何使用 java 锁定文件(如果可能)

我有两个进程调用两个修改相同文本文件的 Java 程序。我注意到文本文件的内容缺少数据。我怀疑当一个 java 程序获取到文本文件的写入流时,我认为它会阻止另一个 java 程序修改它(就像当你打开一个文件时,你不能删除那个文件)。除了数据库,有没有办法解决这个问题?(不是说db解决方案不干净或不优雅,只是我们在操作这个文本文件时写了很多代码)

编辑

事实证明,我针对这个问题犯了一个错误。我的文本文件中的数据丢失的原因是,

ProcessA: 继续将数据行添加到文本文件中

ProcessB: 在开始时,将文本字段的所有行加载到一个List. 然后操作该列表的包含。最后,ProcessB将列表写回,替换文本文件的包含。

这在顺序过程中效果很好。但是当一起运行时,如果ProcessA向文件中添加数据,在ProcessB操作期间List,那么当ProcessBList回时,无论ProcessA添加什么,都会被覆盖。所以我最初的想法是在ProcessBList回之前,同步文本文件和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)

java io

5
推荐指数
1
解决办法
2万
查看次数

我们可以同时打开多个FileWriter流到同一文件吗?

我们可以同时将多个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出来了,我在数据文件中得到了12

java

5
推荐指数
1
解决办法
8366
查看次数

如何使用java.nio.channels.FileChannel读取ByteBuffer实现类似BufferedReader#readLine()的行为

我想使用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)

java nio bytebuffer filechannel

5
推荐指数
1
解决办法
7328
查看次数

Java EE 6:如何将ServletContext注入托管bean

(使用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 …

jsf servlets java-ee java-ee-6

5
推荐指数
1
解决办法
1万
查看次数

IE8和Primefaces p:selectOneMenu在表格中有很多p:selectOneMenu时行为不端

首先,我想为我发布的长代码道歉,它们非常简单,它只是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)

css jsf internet-explorer selectonemenu primefaces

5
推荐指数
2
解决办法
7957
查看次数