I've included an html in my JSP
< s: include value="test.html" />. this test.html contains radio button. How do i retrieve the value of the radio button and pass it to action?
Add this <s:include /> tag in a form in your main page. e.g.
<s:form action="Welcome">
<s:include value="test.jsp"></s:include>
<s:submit />
</s:form>
test.jsp:
<input type="radio" value="abc" name="test" >abc</input>
<input type="radio" value="def" name="test" >def</input>
And in your Action named Welcome :
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println(request.getParameter("test"));
You can get the value of your radio button. Hope this helps!
<s:form action="sampleAction">
<s:include value="yourjspfile.jsp"/>
<s:submit />
</s:form>
yourjspfile.jsp
<s:textfield name="name" label="Enter your name"/>
You can specify the name property in your action class and can access it directly no need to get it from the request object.
Related
For each form in an HTML page I want to add an additional hidden input tag as it passes through a JEE Filter. For a given HTML page for example:
<html>
<form name="input" action="submit.jsp" method="post">
<input type="hidden" name="id" value="1"/>
<input type="submit" value="Submit"/>
</form>
</html>
the end result should be similar to this:
<html>
<form name="input" action="submit.jsp" method="post">
<input type="hidden" name="id" value="1"/>
<input type="submit" value="Submit"/>
<input type="hidden" name="dynamickey" value="DYNAMIC_VALUE_HERE"/>
</form>
</html>
As the HTML may be malformed, I am figuring that Jericho would be the HTML parser of choice. After a couple of passes through the web pages, I have found ways to change the values of already existing tags, but how to add an additional tag escapes me.
Thanks in advance for help.
I created a simple JSF login page, and I'm trying to integrate it with spring security.
Here is the form element from login.xhtml
<h:form>
<h:outputLabel value="User Id:" for="userId"/>
<h:inputText id="j_username" label="User Id"
required="true" value="#{loginBean.name}" >
</h:inputText>
<h:outputLabel value="Password: " for ="password"/>
<h:inputSecret id="j_password" value="#{loginBean.password}" />
<h:commandButton value="Submit" action="#{j_spring_security_check}" />
</h:form>
But the rendered html page has something like the below. Take a look at the form action and the input tag's names
The form element
<form id="j_idt6" name="j_idt6" method="post"
action="/jsfproject2/faces/login.xhtml"
enctype="application/x-www-form-urlencoded">
And the input tags
User Id:</label><input id="j_idt6:j_username" type="text"
name="j_idt6:j_username" />
Now I want form action to be /j_spring_security_check and input boxes to be 'j_username' and j_password
How can we achieve this ?
There are two options for Spring Security to work.
Use prependId="false" on a JSF form
As <h:form> is a naming container, it prepends id of its children with the specified id, or the autogenerated id, so as Spring Security expects ids to remain unchainged, just don't prepend the ids:
<h:form prependId="false">
<h:outputLabel value="User Id: " for="userId" />
<h:inputText id="j_username" label="User Id" required="true" value="#{loginBean.name}" />
<h:outputLabel value="Password: " for ="password" />
<h:inputSecret id="j_password" value="#{loginBean.password}" />
<h:commandButton value="Submit" action="#{loginBean.login}" />
</h:form>
Note that #{j_spring_security_check} is a wrong action method: it needs to be #{loginBean.login} with the following contents:
public String login() {
//do any job with the associated values that you've got from the user, like persisting attempted login, etc.
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext extenalContext = facesContext.getExternalContext();
RequestDispatcher dispatcher = ((ServletRequest)extenalContext.getRequest()).getRequestDispatcher("/j_spring_security_check");
dispatcher.forward((ServletRequest)extenalContext.getRequest(), (ServletResponse)extenalContext.getResponse());
facesContext.responseComplete();
return null;
}
Basically, all you need to do is dispatch to /j_spring_security_check and have j_username and j_password as request parameters.
Use plain HTML form
Basically, there's no particular need to mess with JSF form on this issue, in case you don't need to do some extra things apart from authentication, and plain HTML form is sufficient for Spring Security to do its job.
<form action="/j_spring_security_check" method="POST">
<label for="j_username">User Id: </label>
<input id="j_username" name="j_username" type="text" />
<label for="j_password">Password: </label>
<input id="j_password" name="j_password" type="password"/>
<input type="submit" value="Submit"/>
</form>
Thats how i did it:
<form action="${request.contextPath}/appLogin" method="POST">
<h:form prependId="false">
<p:inputText id="app_username" placeholder="Username" name="app_username" styleClass="Wid80 TexAlCenter Fs18" required="true"/>
<p:password id="app_password" placeholder="Password" name="app_password" label="Password" required="true" styleClass="Wid80 TexAlCenter Fs18"/>
<button type="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only Wid60">
<span class="ui-button-text">Login</span>
</button>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</h:form>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
I'm using the Premium Theme Ronin from Primefaces and custom login URLs like "appLogin" and custom Parameters like "app_username".
I am using Razor to generate a form. I want to create HTML elements based on some value from it's model property.
for example, if a model contains Id property, and I want to generate html tags as follows
<input type="hidden" name="1_chk" />
<input type="hidden" name="2_chk" />
<input type="hidden" name="3_chk" />
So I used the following syntax, and it failed. Can anyone help me out with this?
<input type="checkbox" name="#Id_chk" />
Thanks
I think this should work for you:
<input type="checkbox" name="#(Id)_chk" />
another option:
<input type="checkbox" name="#(Id + "_chk")" />
I have a form in which i'm dynamically addding controls through jQuery. I need to access the values in those controls (textboxes) when posting back the form to the server. I'm sure this is a trivial problem but i just can't get my head around it.
Any help would be greatly apreciated.
When adding a multiple controls to the page, give them all the same name attribute so you can do the following in your action:
public ActionResult MyAction(string[] items)
{
// items will contain all the values in the text boxes
return View();
}
So your HTML would like like this
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
I am working on a struts2 application. In my jsp page I have 2-3 anchor tag and 2-3 hidden fields like
<s:a href="#">File 1</s:a>
<s:a href="#">File 2</s:a>
<s:a href="#">File 3</s:a>
and
<s:hidden name=" hidden1" />
<s:hidden name=" hidden2" />
<s:hidden name=" hidden3" />
Now please let me know, In my action class how can I get value of all the hidden fields and anchor tag which was clicked.
I had tried following
<s:a href="#" action=”someAction”>File 1</s:a>
Its working but didn’t transfer value of hidden fileds.
Also
<s:a href="#" name=”File1” onclick=”submit”>File 1</s:a>
But no gain.
Looking for your reply.
Like Boris said, you need to put the hidden fields inside a form, and submit that form, OR you can add them as URL parameters to your links. The best method is probably using a form with POST so the hidden fields aren't on your browser's location bar.
Here's an example
<s:form id="myform" name="myform" action="someAction" method="POST">
<s:hidden name=" hidden1" value="first value"/>
<s:hidden name=" hidden2" value="second value"/>
<s:hidden name=" hidden3" value="third value"/>
Submit with link
<s:submit value="%{'Submit with button'}" />
</s:form>
Since this really has nothing to do with struts2, here's an example with pure HTML:
<form id="myform" name="myform" action="someAction.action" method="POST">
<input type="hidden" name=" hidden1" value="first value"/>
<input type="hidden" name=" hidden2" value="second value"/>
<input type="hidden" name=" hidden3" value="third value"/>
Submit with a link
<br/>
<input type="submit" value="Submit with a button"/>
</form>