Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I retrieve the screen width so I can plan the layout of the display  RSS feed

 
Jan Boeyens
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In normal html I can quite simply do this:



I want to retrieve this information (possibly on the first page that runs) - and then use the width to calculate subsequent page control positions.
(JSP does not allow me to center controls reliably - so I want to calculate their exact positions instead)

I know everyone says that JSP and Javascript can't communicate etc. etc. But at the point when the first page is submitted - the information is available to javascript. Also at that point the JSP is able to retrieve information from the page (such as the value of text controls etc.). I need to know how do I get it from the function into a bean or on-page variable.value (so that I can retrieve it and use it before calling the next pages).

I have a basic outline of what I plan:

JSP:


Then I thought I could retrieve it in the action event of a button (or in the Destroy() event) as such:
Java:

String s;
s=new String(""+hid1.getValue());


Any help or suggestions would be most welcome !



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Boeyens wrote:In normal html I can quite simply do this:


Just because your HTML page is being generated by a JSP in no way affects how it is handled at the browser. SO the result of executing the JSP is "normal HTML".

You can use CSS in a JSP-generated JSP in the exact same way as in "normal HTML", because it is "normal HTML"
 
Jan Boeyens
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the code fix !

I understand that the "final product" is still pure html. But I don't understand how to access the "width" variable (either in JSP or in Java).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't. Why try to do on the server what should be done in the client? Do it the same way as if you were just writing the HTML yourself.
 
Jan Boeyens
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to have the value of the "width" so that I can adjust the settings on subsequent pages. How do I get the value returned by the javascript function to the server for use during subsequent page generation ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, you shouldn't. You should be using modern CSS techniques and not using the width and trying to absolutely position things.

I thought you said you were using the width because you couldn't do things as you would normally do in HTML. Well, you should just do the "normal" things.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But to answer the question anyways, even though I think you are heading in exactly the wrong direction: JavaScript values can be sent to the server via requests, just like any other client-side value.
 
Jan Boeyens
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear

Would you mind giving me an actual example.
The application is Java faces - so the typical way of getting results is to use:


How do I do the JSP side to get the value into the "hid1" control ? (My original query had everything except the missing step)


I accept that there could be better ways of doing this - but the problem is that this is a huge legacy system - that needs to be adjusted - and this seems the simplest way forward.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to ask JSF questions in the JSF forum. I have moved this post there for you.
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If you do this, the javascript will update the hidden form field with the current window screen width, then submit the form. The JSF lifecycle will validate the form and (assuming the form data is all valid) update the "screenWidth" property value in the backing bean so that the adjustScreen action method can then make use of it.

Beyond that, I do not recommend using bindings or listeners in JSF unless you absolutely have to. They make for more complicated logic and less flexibility and usually aren't necessary, since JSF was designed to do as much as possible using POJOs and POJO techniques and not framework-specific stuff.

Also note that the "onclick" returns a value. If you omit the value and for any reason it returns false, the submit will be suppressed at the browser (this is basic HTML, not JSF).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!