Struts2 text value displayed at incorrect position - java

I have created a struts2 login page, wherein I have all basic components necessary for login. I'm using struts2 text tag for a label. Below is my login page code snippet.
<body>
<h2>Demo - Login</h2>
<s:actionerror />
<s:form action="login.action" method="post">
<s:textfield name="username" key="label_username" size="20" />
<s:password name="password" key="label_password" size="20" />
<s:submit name="signIn" key="label_login" align="center" />
<s:text name="name_msg"/>
<s:submit name="signUp" key="label_signUp"></s:submit>
</s:form>
</body>
I always see that text(New to Demo ?) is displayed after heading, as shown in below Image. There text is read from MessageBundle. I tried by giving some direct text value, despite of referring to resource bundle, even though same result. Where I was wrong.

The <s:text> tag displays text with no "decoration".
The default "xhtml" theme's form tags emit table markup.
This means you're currently generating invalid HTML, so the text will show up in essentially arbitrary locations based on how the browser handles stuff showing up in between table rows.
Viewing the source would have answered this question immediately.
You need to either put the text into a table row, as with everything else in the form tag, or use the "simple" theme and do all the layout, error messages, etc. yourself.

Related

get value of attribute outside form tag from jsp to servlet

i have one view where i m showing many records and now i m adding functionality for user to download those records which he will choose date from search textbox. As my search textbox in jsp outside of form tag and thus i m not getting the value of that parameter in my servlet..is there any way to get that value from outside of form tag? Here is my jsp
<div id="divOfDateTable">
Search:<input type="text" name="dropdown" id="datedropdown">
<button id="dateButton" name="dateSearch">Search</button>
</div>
<form action="Download_Servlet" class="download" method="post">
<input type="submit" id="downloadRecords" value="Download Order-records">
</form>
In my Servlet i want to get that parameter value.i.e;Date put by user.As per the requirement only the searched records by the user needs to be downoaded. So please guys help out..

Struts 2 and HTML tags?

I have a form in Struts2 as follows:
<s:form >
<s:select list="#session.circleIdNameMap" label="Select Circle:"
headerKey="-1" headerValue="Select Circle"
id="selectCircleDropDown" onchange="getTspList(this.value)"></s:select>
<select id="selectTspDropDown"></select>
<s:radio list="#{'0':'Default','1':'Latest'}" label="Select Threshold type:"
name="flag" id="flag"></s:radio>
<s:submit type="button" onclick="getThresholdData();return false;" />
</s:form>
Here I am using HTML <select> tag in between, due to which there is an alignment issue. The second select tag comes up. Using simple theme everything comes in a single line.
What should I do to make alignment right?
The HTML <select> tag is used with the <option> tags together. See the example.
But you could use Struts select tag where the options are generated automatically from the list values.

How to get form name value in JSP

Is there a way to get the name of the form element itself using JSP? I searched google and did not find any examples which show how to get the name of the form value specified in the JSP file.
For example, I have the below form,
<html>
<form name="register" action="servregister" method="POST"
onsubmit="return validate();">
</form>
</html>
I would like to get the string "register" to a string variable in JSP. Is there a way to do this?
No, a <form> is not submitted with the request. Instead create a hidden input element which holds that information.
<input type="hidden" name="name of form" value="value" />
or possibly the submit element
<input type="submit" name="distinguishing name" value="submit" />
Either of these in a form with be sent as a url-encoded parameter.
There is probably a better solution to what you are trying to do if you explain your goals.
Consider looking into different patterns for achieving your goals.

JSP / servlet / IE combination doesn't submit form detail on Enter

Using IE 7, JDK 1.6 and Sun Web server 7.
Inside the jsp form, we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="send" value="Send"/>
i.e. a text box and a "Submit" button (called Send).
and the servlet has:
if (request.getParameter("send") != null && request.getParameter("send").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent when we populate the id text box and hit Enter:
id=123456
However, using Fiddler and IE, we can see that the following is sent when we populate the id text box and click the Send button:
userId=123456&send=Send
The end result is that hitting the Enter key effectively does nothing.
On other jsp pages, e.g. we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="submitId" value="Submit"/>
and the servlet has:
if (request.getParameter("submitId") != null && request.getParameter("submitId").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent for both cases:
id=123456&submitId=Submit
So it seems to us that the behaviour is only exhibited on IE for forms where the "Submit" button is not called "Submit"?
Re-running the tests on Firefox 3.6 shows that the behaviour is correct and the same for both cases.
Any suggestions for getting IE to work correctly?
(Note: I have searched SO for a similar problem but the questions relating to this mainly all ASP related!).
This is indeed another IE anomaly in case of forms with only one input field. The only solid workaround for this is to add a second input field(!). You can hide it using CSS. No, type="hidden" ain't going to work.
<input type="text" name="id" maxlength="20" />
<input type="text" style="display: none;" />
<input type="submit" name="send" value="Send"/>
Why are you checking for request.getParameter("submitId") in your JSP when in fact submitId is the name of your submit button?
In my experience I never had to check the value for the submit button. I only used that button to trigger the form submit and would usually only be interested in retrieving the values for the other form parameters.
If you want to differentiate the submit methods by the name of the submit button, you might want to try adding a "hidden" property using input type="hidden".

Using spring:message to define form tag attribute in Spring web application

I'm developing a Java/Spring web application. The problem I'm currently facing is that I'd like to have message from message.resources shown as an attribute in an HTML.
<input type="submit" name="login" value="login" />
So instead of the hardcoded value "login" I need to the value of
<spring:message code="general.submit" /> as the value attribute of that input tag. As the pages are all xml, it's no option to nest tags like
<input type="submit" name="login" value="<spring:message code="general.submit" />" />
as it does not compile. I could, of course, read the value in the Java controller and use a JSTL variable to display the value, but I think it would be too hackish and complicated, especially for pages with large amount of submit buttons. Is there some kind of elegant way of accomplishing what I want to do?
Use <spring:message> to store the value in a var, then reference that var using EL, e.g.
<spring:message code="general.submit" var="submitText"/>
<input type="submit" name="login" value="${submitText}" />

Categories