Vaadin with using hibernate

Good afternoon.

It is necessary to realize a small web application in a linking of vaadin hibernate, in application there is TreeTable, textfield
for adding or parent item or child (in case of a choice it is also necessary to specify the parent in a checkbox).

Prompt as it is correct to realize actions for adding of Entity in a basis of h2 and up-dating of the table in the interface.
I read that for TreeTable it is possible to use DataSource jpacontainer.

On official the website it is specified that it is necessary to create EntityManagerProvider for Hibernate.

To make my assumption so – there are two entities mapping on two tables and the persistence.xml file.
After input in a checkbox to read data and to create such object further to create JPA container with objects and then to throw it in TreeTable.
But I think that options shall be much better…

How to realize adding and display to UI of these entities it is correct with use of hibernate?

Jersey chooses wrong ExceptionMapper

Currently I’m using jersey 2.25 version and rs-api 2.0. So here is a problem.
I have a resource which can throw javax.ws.rs.NotAcceptableException and 2 exception mappers:

DefaultExceptionMapper implements javax.ws.rs.ext.ExceptionMapper<java.lang.Throwable>
WebApplicationExceptionMapper implements javax.ws.rs.ext.ExceptionMapper<javax.ws.rs.WebApplicationException>

From logs I observe both providers successfully registered.
So far as NotAcceptableException extends ClientErrorException extends WebApplicationException I expect triggering of WebApplicationExceptionMapper but it never happens. DefaultExceptionMapper always intercepts control. Any ideas? Thanks.

Format XML output/modify Transformer in apache commons-configurations2

I am trying to migrate from the old commons-configuration to commons-configuration2 but I am having trouble formatting the XML output with indentation when using the new Configurations builder.

Before I did like this, which worked fine.

XMLConfiguration configuration = new XMLConfiguration()
{
    @Override
    protected Transformer createTransformer()
        throws ConfigurationException
    {
        Transformer transformer = super.createTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("http://xml.apache.org/xslt}indent-amount", "4");
        return transformer;
    }
};

But in commons-configurations2 you use a ConfigurationBuilder to get a XMLConfiguration instance, which removes the ability to create a subclass of XMLConfiguration, for example like this:

XMLConfiguration configuration = configurations
        .xmlBuilder(new File("config.xml"))
        .getConfiguration();

Is there any other way of customizing the XMLConfiguration’s Transformer?

Thanks!

Long menu item in Java AWT application not visible

When I start Java application with AWT GUI on remote server that uses my local X-server (Ubuntu 16.04 with Unity), menu items with long names are not visible. It is very interesting that I can see this behavior only on one specific server whilst it works on others.

Demonstration code is:

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Main extends Frame {

    public Main() {
            this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
            });
        Menu mn = new Menu("File");
        MenuItem mi = new MenuItem("It is very very long text");
        mn.add(mi);
        mi = new MenuItem("long");
        mn.add(mi);
        MenuBar mb = new MenuBar();
        mb.add(mn);
        this.setLayout(new BorderLayout());
        this.setMenuBar(mb);
        this.setSize(200,200);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
    }
}

Testing struts request parameter against String with odd results – why does it look like a collection?

I am calling an action via foo.action?error=bar

In struts.xml, I have configured the action chain as follows:

<action name="foo">
  <interceptor-ref name="defaultStack"/>
  <!-- custom interceptors -->
  <result name="success">/jsp/foo.jsp</result>
</action>

In the JSP, I’m running a test:

<s:if test="#parameters.error[0] == 'bar'">

It’s legacy code; this works.
However, the following does not, and I don’t understand why:

<s:if test="#parameters.error == 'bar'">

Why do I need to pretend the error parameter is a collection?
Judging from the docs, I shouldn’t have to – but then I haven’t used JSPs much…

I have added the following to the JSP, in order to help me understand what’s going on:

<s:property value="#parameters.error"/>                          // bar
<s:property value="parameters.error"/>                           // <nothing>
<s:property value="parameters.error[0]"/>                        // <nothing>
<%= pageContext.getRequest().getParameter("error") %>            // bar
<%= pageContext.getRequest().getParameter("error").getClass() %> // class java.lang.String
<s:property value="#parameters.error=='bar'"/>                   // false
<s:property value="'token'.getClass()"/>                         // <nothing>

That output confuses me even more. Can someone please explain what’s going on?

Java count how many times a char from string occurs from an array [duplicate]

This question already has an answer here:

I’m new to java and I cant find the answer to this problem.

If i have two Arrays like this with counters:

Character[] abc = {'A','B','C'};
int countABC = 0;

Character[] def = {'D",'E','F'};
int countDEF = 0;

And a string like this:

String something = "ABCDEFGHABAB"

How to increase the counters?

Exception in thread “main” java.lang.NumberFormatException: For input string:

I am working on the knapsack problem, I am new to Java. I am able to add numbers manually like this in the main:

 // Fill the bag of weights. 
 //myWeights.bagOfWeights.add(18);
 //myWeights.bagOfWeights.add(2);
 //System.out.println("Possible answers: ");
 //myWeights.fillKnapSack(20);

However, I am not able to allow the user to input the numbers. The first number should be the target followed by the weights. So I have tried to take the user input as a string and split it up with whitespace, and then convert it to an integer. Next, I tried to do the parseInt 2 ways, but I was unsuccessful both way. Here is the code:

import java.util.*;

public class KnapSackWeights{

private Sack bagOfWeights = new Sack();
private Sack knapSack = new Sack();

public static void main(String[] args){
    KnapSackWeights myWeights = new KnapSackWeights();
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the input:");
    String input = in.nextLine();
    String[] sar = input.split(" ");
    //System.out.println(inp);
    int target = Integer.parseInt(input);
    System.out.println(target);

    int[] weights_array = new int[26];

    int n = input.length()-1;
    for(int i=1; i<=n; i++)
    {
       weights_array[i - 1] = Integer.parseInt(sar[i]);
    }
    int k = weights_array[0];
    myWeights.bagOfWeights.add(target);
    //System.out.println(target);
    System.out.println("Possible answers: ");
    myWeights.fillKnapSack(k);
    //myWeights.fillKnapSack(Integer.parseInt(sar[0]));

    // Fill the bag of weights. 
    //myWeights.bagOfWeights.add(11);
    //myWeights.bagOfWeights.add(8);
    //myWeights.bagOfWeights.add(7);
    //myWeights.bagOfWeights.add(6);
    //myWeights.bagOfWeights.add(5);
    //myWeights.bagOfWeights.add(4);

    //System.out.println("Possible answers: ");
    //myWeights.fillKnapSack(20);
}


public void fillKnapSack(int targetWeight){
    knapSack.empty();
    fillKnapSackRecursion(targetWeight, 0);
}

public void fillKnapSackRecursion(int targetWeight, int i){

    // Finish recursion 
    if(i == bagOfWeights.size())
    {
        // It means we have found good solution 
        if(targetWeight == 0){
            System.out.print("Solution: ");
            for (int j = 0; j < knapSack.size(); j++){
                System.out.print(knapSack.getWeight(j) + " ");
            }
            System.out.println("");
        }
        return;
    }

    // You can do two moves now, add i-th item or continue packing withou i-th item 
    knapSack.add(bagOfWeights.getWeight(i));
    fillKnapSackRecursion(targetWeight - bagOfWeights.getWeight(i), i+1);
    knapSack.remove(knapSack.size()-1);
    fillKnapSackRecursion(targetWeight, i+1);       
}


class Sack extends ArrayList{

    public Sack(){
        super();
    }

    public void add(int weight){
        add(new Integer(weight));
    }


    public int getWeight(){
        int sackWeight = 0;
        for (int i = 0; i < size(); i++){
            Integer nextWeight = (Integer)get(i);
            sackWeight = sackWeight + nextWeight.intValue();
        }
        return sackWeight;    
    }

    public int getWeight(int n){
        int w = -1;
        if (n < size()){
            //Integer weight = (Integer)get(n);
            Integer weight = (Integer)get(n);
            w = weight.intValue();
        }
        return w;
    }

    public boolean isEmpty(){
        return (size() == 0);
    }

    public void shuffle(){
        Collections.shuffle((List)this);
    }

    public void empty(){
        clear();
    }

}

}

Here is the error:
Exception in thread “main” java.lang.NumberFormatException: For input string: “18 7 4 6”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at KnapSackWeights.main(KnapSackWeights.java:18)

Thanks for your help.

No documentation for methods in Eclipse no JDK

I know this was dealt with here: Missing basic JavaDoc for methods in Eclipse – however the recommendation supplied there is an incredible non-answer as to why this happens.
Since the documentation does contain the documentation on methods, so it should be straightforward for Eclipse to extract the information from the same location.
Okay, so once again: use the JDK is not a helpful answer – or an answer at all – and I know there has been a thread on this question before.

Having said that: here’s my situation. I am using only the JRE in Eclipse. I downloaded the documentation on Standard and FX and attached them via Eclipse. However, Eclipse only displays tooltips on classes, not on methods. Why is that? How could I change that?