Wednesday, April 20, 2011

How to get textbox value from client side and create query string from it?

I'm using jquery for modal dialogs. I want to open model dialog from one page and to send some additional query string to modal dialog page. something like this:

 <asp:HyperLink ID="hypClientSearch" runat="server" NavigateUrl="~/SomePage.aspx?KeepThis=true&additionalQS='<%= txtBox.Text %>'&TB_iframe=true&height=650&width=800&modal=true" CssClass="thickbox" >

This example doesn't work. Does anyone know the solution?

From stackoverflow
  • Try this in the modal dialog when it opens (this is client side javascript):

    var textBoxValue = window.opener.document.getElementById("txtBoxId").value;
    

    You then use Javascript to insert the additional info into the correct place in the dialog, for example using JQuery.

  • In addition to Helgi's answer.
    If you want to get the value of the textbox using jQuery (for when you need to use other selectors then the id) you can use:

    var textBoxValue = $(textBoxSelector, window.opener.document).val();
    

    Edit
    Oh I just noticed that your using modal. Then the page is opened in an iFrame, you can get the value from within the iFrame using:

    var textBoxValue = $(textBoxSelector, window.parent.document).val();
    

    Also if you need to send it to the server at the iFrame request try editing the href attribite of the link on click:

    $('#hypClientSearch').click( function() {
     var textBoxContent = $(textBoxSelector).val();
     $(this).attr('href', 'somepage.aspx?textbox='+textBoxContent+'&otherVarsForModal=foo');
     //we let the event bubble for the modal plugin, so ne returning false here
    });
    

Website localization depending on its extension

Hello,

I'm currently building my personal website in ASP.Net MVC and I want two versions: a French and an English one.

I've got two domain names and I want to match the English version to the ".com" domain and the French version to the ".fr" domain:

  • When you go to www.mywebsite.fr, you will get the French version
  • When you go to www.mywebsite.com, you will get the English version

So, my question is: What is the best (easiest?) way to handle this?

Thanks

From stackoverflow
  • A quick overview: use ASP.NET Localization infrastructure to enable localization (there's plenty of stuff for localizing ASP.NET MVC-based sites) and add section to your Web.config that would map .com to en-US, and .fr to fr-Whatever.

Automated tests: mocking vs creating test object graph ( using IoC container), what is better under what conditions?

How do you decide what to choose:

  1. use mock objects for a test OR
  2. create a test object/ object graph using an IoC framework and run test on that data
From stackoverflow
  • I am very happy with using IoC for much of my app, and especially I appreciate that test-datasources can be injected for testing.

    For more problematic backend connections (currently a single ESB call) or functions that need complicated state I mock.

  • For unit tests: If an object is not the object tested, mock or stub it. In that way, you can directly control it so it returns the data that you want.

    If you create a test object/object-graph, you have to set it up so that it provides the data that you want. That is probably a lot more work than you want.

    For integration tests, of course you'd test a whole object graph at a time.

  • If you need to write a lot of initialization code - a mocking framework would probably help you write better, easy to understand Unit Tests.

    There is no need to re write code that a mocking framework can save you.

  • It depends what you are trying to test. Unit tests with collaborators mocked out are great because

    • They are really, really fast
    • They are small and easy to understand
    • They don't have dependencies on the wider world which makes them easy to run
    • They provide excellent defect localisation

    However, pure unit tests cannot tell you if you have configured your objects correctly in your IoC container, if the database connection string works etc. You need a test which runs up your IoC container and really reaches out to the Db to prove these things.

    If you write as many of your tests as pure, standalone unit tests as possible then your build will stay fast. This is crucial, as a slow bulid gets run less. Even so, don't forget to add a sprinkling of wired tests to prove that your application'hangs together'.

    For example, we have a (single) test for every service in our container that proves that we can request it from the IoC container. This proves we're wired up, from then on it is unit tests all the way. We have lots of pure unit tests.

    The whole lot is then wrapped in some application level functional tests to prove that the app itself does what the user wants.

    The thing to bear in mind is the time cost of each test type. Moving from pure unit -> wired -> functional tests costs an order of magnitude of execution time and complexity when they break.

problem with using XPath to locate some part of XML

Duplicate of

HOw to make the text repeat only once every for-each?

I have problem with using XPATH to point to some element in XML. Here is my XML code:

    <character>
      <name>some name </some name>
    </character>

  </quote>
 <quote>
  ....
  </quote>
 </quotes>

and here My XSLT code:

<xsl:for-each select="quotes/quote">
    <xsl:value-of select="quotes/quote/character/name"/>
 </xsl:for-each>

WHat I try to do is that I try to list the name of character for every quote. BUt this code does not work WOuld you please help me with this problem? Thank you

From stackoverflow
  • The error is:
    You use quotes/quote twice

    You might try this one:

    <xsl:for-each select="quotes/quote">
      <xsl:value-of select="character/name"/>
    </xsl:for-each>
    
  • Inside the for-each, the context node is quote - so it looks like you just need:

    <xsl:for-each select="quotes/quote">
        <xsl:value-of select="character/name"/>
    </xsl:for-each>
    

    Of course, the purists would argue that it would be better to use a match:

    <xsl:apply-templates select="quotes/quote" />
    ...
    <xsl:template match="quote">
        <xsl:value-of select="character/name"/>
    </xsl:template>
    

send form data as web service in symfony

I am making a restrictive portal to a WiFi network using symfony, and I want to send a form as web service to other sites that want to use this portal. How should I solve this? I realize I could go the SOAP/WSDL route, but since symfony is already RESTful, it seems to me I could go the RESTful route with considerably less pain and loss of performance.

Right now, I have a working form, but I've only made a casual attempt to bring the form to a remote site by using cURL. The form does not work remotely since the routing is not set up correctly (I think). Can someone help me with this?

Thanks.

From stackoverflow
  • Hello,

    to call restfull service somewhere you simply call its URL and read result. Do you have any example of what exactly is not working? Does "other" service support restfull calls? :) I hope so.

    Calling rest service is really as easy as opening a website with some parameters in URL. So I am wondering what should not work. You can use some simplexmlreader or such to read restful services directly if you don't have blocked access to that remote service.

FindBugs not accepting bcel.jar in ANT script

I installed findbugs into my ant lib directory and added the following code into my main ANT script:

<target name="findbugs" depends="init">

    <findbugs home="C:\\findbugs\\" output="html outputFile="C:\\findbugs\\out.html" jvmargs="-Xms512M">
        <sourcePath path="${messageaggregator.src}" />
        <class location="${messageaggregator.src}"/>


    </findbugs>
</target>

The following xml is called within the init target:

<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">

On running the ANT script, all I get is the following output:

findbugs:
    [findbugs] Executing findbugs from ant task
    [findbugs] Running FindBugs...
    [findbugs] BCEL class compatability error.
    [findbugs] The version of class org.apache.bcel.generic.ObjectType found was not compatible with
    [findbugs] FindBugs.  Please remove any BCEL libraries that may be interfering. This may happen
    [findbugs] if you have an old version of BCEL or a library that includes an old version of BCEL
    [findbugs] in an "endorsed" directory.
    [findbugs] Output saved to C:\\findbugs\\out.html

Why is findbugs not working?

From stackoverflow
  • You've got a conflict with an older version of BCEL that you have to get rid of. It might be in your jre/lib/ext directory (bad idea), or part of the CLASSPATH that you've got for your project, or maybe part of the Ant /lib. In any case, you should find all the BCEL JARs in your CLASSPATH, remove them, and update them with the version that FindBugs requires.

  • If findbugs has its own version of BCEL then why do I get this error:

    [findbugs] Executing findbugs from ant task
    [findbugs] Running FindBugs...
    [findbugs] The java class is not found:  org.apache.bcel.classfile.ClassFormatException
    [findbugs] Output saved to C:\\findbugs\\out.html
    

    This error occurs if I remove bcel.jar from the findbugs.home directory.

  • I have the same problem with class not found. I need to add paths to external jars (jars are placed in different directory than findbug.jar) for findbug inlcuding bcel.jar into its classpath. But even classpath is right, this error is occuring.

  • Java Version 1.6.0_06 contain old BCEL library.

    java version "1.6.0_06" Java(TM) SE Runtime Environment (build 1.6.0_06-b02) Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode)

    Once I change to 1.5.0_17 it works fine for me.

Two-way relation in JPA

Hi

I have the two entity classes, User and MyCharacter. User has a list of MyCharacters and each MyCharacter has a reference back to the User (owner). What I'd like to accomplish is, that I use the same join table for both relations, meaning, that the owner relation found in MyCharacter would automatically use the same join table as from User=>MyCharacter. This means that the getOwner() method in MyCharacter should work without me having to explicitly at some point call setOwner(user).

To clear this a bit more, here's my unit test which currently fails (last assert fails)


@Test
public void testTwoWayRelation() {
    User user = new User();
    MyCharacter character = new MyCharacter();
    List<MyCharacter> chars = new ArrayList<MyCharacter>();
    chars.add(character);
    user.setCharacters(chars);

    facade.store(user);
    assertNotNull(character.getId());

    character = facade.find(MyCharacter.class, character.getId());

    assertNotNull(character.getOwner());
}

My entity classes are listed below.


@Entity
@Table(name = "myuser")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    @OneToMany(cascade = { CascadeType.PERSIST })
    protected List<MyCharacter> characters;

    public User() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public List<MyCharacter> getCharacters() {
     return characters;
    }

    public void setCharacters(List<MyCharacter> characters) {
     this.characters = characters;
    }

}


@Entity
public class MyCharacter{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    @ManyToOne
    @JoinTable(name = "myuser_mycharacter", joinColumns = @JoinColumn(name = "characters_id"), inverseJoinColumns = { @JoinColumn(name = "user_id") })
    protected User owner;

    public MyCharacter() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public User getOwner() {
     return owner;
    }

    public void setOwner(User owner) {
     this.owner = owner;
    }
}
From stackoverflow
  • Here is an article from Oracle which explains how to map such relations.

    When you use this in Java, depending on your JPA framework, you will need to add the MyCharacter to the list in User and set the user field in MyCharacter or only one of the two (because the framework will manage the other side for you). I suggest to write a small test to figure out what works (and you should write test cases for all the way in which you use your objects, anyway).

    When objects are loaded from the database, you won't need to do that since all frameworks handle this case correctly.

  • That's how we join two entities with jpa in our project:

        @Entity
        @Table(name = "Period")
        public class Period implements Serializable {
          private List<Delay> delays = new ArrayList<Delay>();
    
          @OneToMany(mappedBy="period") //name of the field in joined entity
          public List<Delay> getDelays() {
          return delays;
          }
        }
    
       @Entity
       @Table(name = "Delay")
       public class Delay implements Serializable {
    
        private Period period;
    
        @ManyToOne
        @JoinColumn(name = "PERIODID")
        public Period getPeriod() {
           return period;
        }   
    }
    
  • i'm not sure i understand your problem correctly, but you could try to set mappedBy on MyCharacter.owner:

    @ManyToOne(mappedBy="characters")