How to get a value that is sent by a form in jsp.
This is because you are not sending Name back to the controller.for sending values back to the action all you need to send them as form fields where as in your case you are just displaying them but not sending them back.
One solution is to store the name as a hidden field like
<s:form action="AddProduct" >
<tr><td>
<label for="name">Name:${Product.name}</label>
<s:hidden name="name" value="%{Product.name}"/>
</td></tr>
<s:submit/>
</s:form>
Make sure you should have name property in your action class or if you have a bean in your action class who has name property than the name of hidden field shd be beanName.name
Related
I'm trying to assign the value of radio button ans1 to another variable ans_submit.
<s:form action="ques2" namespace="/questions" theme="simple">
Question 1: <s:property value="#session.ques1.question"/><br/><br/>
<s:radio list="#session.ques1.option1" name="ans1" value="%{'option1'}" label="1. "/><br/>
<s:radio list="#session.ques1.option2" name="ans1" value="%{'option2'}" label="2. "/><br/>
<s:radio list="#session.ques1.option3" name="ans1" value="%{'option3'}" label="3. "/><br/>
<s:radio list="#session.ques1.option4" name="ans1" value="%{'option4'}" label="4. "/><br/>
<s:submit name="submit" value=" Next"/>
</s:form>
<s:set var="ans_submit" value="%{#ans1}" scope="session" />
But when I retrieve ans_submit in action class, it is giving null.
I have to keep <s:set> tag outside of <s:form> tag. Because there is no variable defined as such in my model.
I don't want to use javascript!
Use a map to populate a radio tag.
<s:radio name="ans1" list="#{'option1': '1. ','option2': '2. ','option3': '3. ','option4': '4. '}" />
In the action class you should have a property for ans1 where the value would be set when you submit the form. This property is local to the action instance which is created every time when you make a new request. So, you need to save the value in the session.
session.put("ans1", ans1);
Now to retrieve this value from session in another action you can initialize the ans1 property
ans1 = (String) session.get("ans1");
or you can preset the value in the radio tag using a value attribute.
<s:radio name="ans1" list="#{'option1': '1. ','option2': '2. ','option3': '3. ','option4': '4. '}" value="%{#session.ans1}"/>
I am assigning href to a button and passing a value to the controller
JSP page
<td><input type="button" name="remove" value="DELETE"/>
</td>
In controller, I am trying to get the value when the button is clicked using:
if(request.getParameter("remove") !=null)
{
int cart_id=(Integer)request.getSession(false).getAttribute("cartid");
b_id = (String) request.getParameter("book_id");
int bid=Integer.parseInt(b_id);
System.out.println(bid);
}
This prints a null value though I have passed book_id value in the URL.
I want to know how can I get the value passed in the URL via the button.
You can't combine [a] tag with an [input] tag like that.
Try using a form instead with hidden inputs:
<form action=viewController>
<input type=hidden name=remove value=delete>
<input type=hidden name=book_id value=<%=vo.getBookId()%>>
<input type=submit value='Delete'>
</form>
The resulting url will be : viewController?remove=delete&book_id=...
When submit button is pressed, the entire form will be sent. You can select where data will be sent by using attribute action:
<form action="demo_form.jsp">
<!--inputs here-->
<input type="submit">Send me now</input>
</form>
In that case form will be sent to demo_form.jsp. In HTML5 you can use formaction attribute if you want use different servlets for different buttons
Any way, you shouldn't use links for sending forms.
It is possible to use links without button:
Remove
I have created a from in JSP page name add.jsp to save data like this
<s:form action="AddDomain">
<s:push value="idp">
<s:textfield name="domainName" label="Domain Name" />
<s:textfield name="url" label="Domain URL" />
<s:textfield name="noOfLicense" label="License Purchased" />
<s:textfield name="licenseExpireDate" label="License Expire Date" title="YYYY-MM-DD like 2013-01-21" />
<s:textfield name="userActiveDuration" label="Active User Duration"
title="please mention in days" />
<s:textarea cols="30" rows="5" name="notes" label="Note"></s:textarea>
<s:submit value="Add"></s:submit>
</s:push>
</s:form>
Action Method that show this view is as
public String addDomainPage() {
return ActionSupport.SUCCESS;
}
I have created another page that list the all domains and provide a edit link to edit any domain. When Use click on edit URL this action is called
public String loadDomain() {
HttpServletRequest request = ServletActionContext.getRequest();
String url = request.getParameter("durl");
IDPBroker broker = new IDPBroker();
idp = broker.getDomainByURL(url);
return ActionSupport.SUCCESS;
}
On successful completion of action I show the add.jsp page. Struts populate the data in JSP page.
Now, issue is that I want to change the value of action attribute of form tag. I also want to change the value of submit button to 'Edit'. I have plan to create some private attribute(action,Label) in Action class and when an addDomainPage action is call I will change the value of these attribute with respect to add page. Similar for loadDomain action. Now I don't know how to do this means how to use these private attributes in view. Tell me is I am doing correctly and what to do next?
The same action class could be used to map different methods on submit buttons. Like
<s:submit value="Add" method="addDomainPage" />
<s:submit value="Load" method="loadDomain" />
The form action attribute should map to the action class execute method which will never call if you use submit buttons like that. The DMI which is enabled by default allows to call specified methods.
If you want to dynamically change attributes in the Struts tags you could use OGNL expressions in JSP instead of hardcoded values. For this purpose you should define properties in the action that define dynamic values before result is executed. For example
public String getAction(){
return "AddDomain";
}
<s:form action="%{action}">
Hi i have a servlet which get a parameter form a jsp on a submit button. One of the parameters is reporting to be null though. However this is not the case. The text input in question is filled automatically by a session variable and is definitely not null and can be seen in the text box on the page. But when inside the servlet the java console indicates that the variable is null? below is the code that populates the box and reads the parameter.
<input type="text" id="cID" value="<%= session.getAttribute("cID")%>" readonly="readonly">
reading the parameter:
String cID = request.getParameter("cID");
On printing cID to the console in netbeans it is reportedly null?
Add the name attribute to the input tag
<input type="text" id="cID" name="cID" value="<%= session.getAttribute("cID")%>" readonly="readonly">
It's the name attribute, not the id attribute, that defines the name of the parameter that's sent to the server. id is purely a client-side thing.
I think you need to write:
<input type="text" id="cID" value="<%= session.getAttribute(\"cID\")%>" readonly="readonly">
I am creating a project in struts2
I have created a Registration.jsp page like below.
<s:form name="registration" action="Registration" >
<s:textfield name="user.userName" label="UserName"></s:textfield>
<s:textfield name="user.userName" label="Password"></s:textfield>
<s:textfield name="user.userName" label="Re-Enter Password"></s:textfield>
<s:textfield name="user.userName" label="Name"></s:textfield>
<s:textfield name="user.userName" label="DOB"/>
<s:textfield name="user.userName" label="email"></s:textfield>
<s:textfield name="user.userName" label="Portfolio Name"></s:textfield>
<s:submit></s:submit>
</s:form>
As seen above I am trying to give the all fields same name and it is used in setting the values..
After calling the action when I am trying to redirect the result to a jsp and trying to display the userName it will display like below
if we input a, b, c, d and e as the parameter respectively in the given fields. I am getting the output exactly(, included in output) like that - Hello a,b,c,d,e..
I am not getting why is this happening.. does anyone has an idea?
This is how client to server communication works.From HTML perspective everything will be sent over to server using key-value pairs of String.
The value being set in the Action class and interpreted as Collection/Array is a feature S2 and its conversion mechanism (OGNL and XWork conversion ).
So when your values being sent as a key with values like user.userName=a,b,c,d,e and you have declared a collection/Array in your action class so S2 type conversion mechanism is coming in to action and converting these values to Array/Collection and setting them in the respected property in your action class.
In the reverse case HTML will know only String and again from the server they are being sent as key and values and since you are not asking your S2 mechanism to come in to play so this is getting printed in your HTML as per your description.
When you submit this page, the url will look like ?user.userName=a&user.userName=b&user.userName=c&user.userName=d&user.userName=e. Struts2 considers this as a Collection or Array. When you take this value to String typed parameter, the values are comma separated and stored. Later on, this is what getting displayed in your page after the action call.