1st problem : i have a jsp in which there are two tabs ,for the second tab i have included the struts include tag. When i open the second tab and performs the action the second tab form values are not passing to the action.
2nd problem : in the select tag i have populate the list but it is coming as prepopulated means all the options are coming as selected="selected"
main jsp in which tabs are made
<!-- admin first tab starts here -->
<form action="saveRolesAndPermissions" method="post">
<table align="center">
<tr>
<td ><s:text name="FTID" ></s:text></td>
<td ><s:textfield id="FTID" name="ft_id">
</s:textfield></td>
<td><input type="button" value="validate" onclick="userPresent()"></td>
</tr>
</table>
<table>
<tr>
<td>First Name</td>
<td><input id="first_name" type="text" readonly onkeydown="return false"></td>
<td>Last Name</td>
<td><input id="last_name"
type="text" readonly onkeydown="return false"></td>
<td>Email-Id</td>
<td><input id="mail_id" type="text"
readonly onkeydown="return false"></td>
</tr>
</table>
<table align="center">
<tr>
<td><s:text name="ROLES"></s:text></td>
<td></td>
<td></td>
</tr>
<tr>
<td><s:text name="Available Roles"></s:text></td>
<td></td>
<td><s:text name="Assigned Roles"></s:text></td>
</tr>
<tr>
<td><s:select id="roles" name="availableRole"
multiple="true" list="availableRole"></s:select></td>
<td><input type="button" value=">" onclick="move_list_items('roles','assignedroles');"/><input
type="button" value="<" onclick="move_list_items('assignedroles','roles');"/></td>
<td><s:select id="assignedroles" name="assignedRole" multiple="true"
list="{}" >
</s:select></td>
</tr>
</table>
<br /> <br /> <br />
<table>
<tr>
<td><s:text name="Permissions"></s:text></td>
<td></td>
<td> </td>
</tr>
<tr>
<td><s:text
name="Available Permissions"></s:text></td>
<td></td>
<td><s:text name="Assigned Permissions"></s:text></td>
</tr>
<tr>
<td><s:select id="permissions" multiple="true"
name="availablePermission" list="availablePermission" headerValue=""></s:select></td>
<td><input
type="button" value=">" onclick="move_list_items('permissions','assignedpermissions');"/><br /> <input type="button"
value="<" onclick="move_list_items('assignedpermissions','permissions');" /></td>
<td><s:select id="assignedpermissions" multiple="true" name="assignedPermission"
list="{}" ></s:select></td>
</tr>
</table>
<br /> <br />
<table align="center" style="width: 25%;">
<tr>
<td><s:if test="hasActionMessages()">
<div class="welcome" style="list-style:none">
<s:actionmessage />
</div>
</s:if></td>
</tr>
</table>
<table>
<tr>
<td><s:submit
value="save"onclick="saveRole();return false;"></s:submit></td>
<td> <input type="button" name="close"
value="close" /></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</form>
<!-- second tab for modify roles -->
<div id="content02" class="content" >
<s:include value="../asp/aspAdminModify.jsp"></s:include>
</div>
<!-- /second tab ends here -->
included jsp
<form action="grantedRolesAndPermissions" method="post">
<table>
<tr>
<td><s:text name="FTID" ></s:text></td>
<td><s:textfield id="modify_FTID" name="modifyftid">
</s:textfield></td>
<td><s:submit
value="search" onclick="search();return false;"></s:submit>
</td>
</tr>
</table>
Bean for the first tab
private String ft_id;
private ArrayList<String> availableRole;
private ArrayList<String> availablePermission;
private ArrayList<String> assignedRole;
private ArrayList<String> assignedPermission;
//getters and setters
Action for the first tab
public class ASPadmin extends ActionSupport implements ServletRequestAware, ModelDriven<ASPAdminBean>{
/**
*
*/
private static final long serialVersionUID = 1L;
private AdminASPService aspService = new AdminASPService();
private HttpServletRequest request = ServletActionContext.getRequest();
private HttpServletResponse response = ServletActionContext.getResponse();
public CMTUser cmtuser;
private ASPAdminBean aspAdminBean = new ASPAdminBean();
//getters and setters
public String populateRolesAndPermissions() throws Exception
{
/*aspAdminBean=new ASPAdminBean();*/
aspAdminBean.setAvailableRole(aspService.getRolesList());
aspAdminBean.setAvailablePermission(aspService.getPermissionsList());
return SUCCESS;
}
public String saveRolesAndPermissions() throws Exception
{
User user = CMTUser.getUser(request);
String login = user.getUserId();
String[] temp = login.split("\\.");
String abcfinal = temp[0].substring(0, 1).toUpperCase()
+ temp[0].substring(1);
String deffinal = temp[1].substring(0, 1).toUpperCase()
+ temp[1].substring(1);
String name = abcfinal + " " + deffinal;
System.out.println("name ==============>" + name);
String id = aspService.saveRolesPermissions(aspAdminBean,name);
System.out.println("id=======>"+id);
if("Y".equals(id)){
addActionMessage("Record Inserted Successfully");
populateRolesAndPermissions();
return SUCCESS;
}
else{
return ERROR;
}
}
#Override
public String execute() throws Exception {
String url = request.getRequestURL().toString();
String[] actionArray = url.split("/");
String event = null;
String forward = SUCCESS;
for (int i = 0; i < actionArray.length; i++) {
event = actionArray[i];
}
System.err.println(event);
try {
if (event.equals("aspAdmin.action")) {
populateRolesAndPermissions();
}
}catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
#Override
public void setServletRequest(HttpServletRequest req) {
this.request = req;
}
#Override
public ASPAdminBean getModel() {
// TODO Auto-generated method stub
return aspAdminBean;
}
}
bean for second tab
public class ASPAdminModifyBean {
private String modifyftid;
private String aspmodifyassignedRole;
private String aspmodifyassignedPermission;
private String createddate;
private String createdby;
private String updateddate;
private String updatedby;
private String username;
private ArrayList<String> modifyavailableRole;
private ArrayList<String> modifyavailablePermission;
private ArrayList<String> modifyassignedRole;
private ArrayList<String> modifyassignedPermission;
//getters and setters
action for the second tab
public class ASPadminModify extends ActionSupport implements ServletRequestAware, ModelDriven<ASPAdminModifyBean> {
private AdminASPService aspService = new AdminASPService();
private GetRolePermissionListInt[] roleVO;
private HttpServletRequest request = ServletActionContext.getRequest();
private HttpServletResponse response = ServletActionContext.getResponse();
private ASPAdminModifyBean modify = new ASPAdminModifyBean();
GetRolePermissionListInt role;
//getters and setters for the above
public String grantRolesPermissions(){
System.out.println("enter the grant roles and permissions");
String tab2="modify";
boolean id;
HttpServletRequest request=getRequest();
HttpSession session=request.getSession();
session.setAttribute("tabs",tab2 );
role=aspService.getGrantedRolesAndPermissions(modify);
System.out.println("assigned roles===================>"+role.getAssignedRoles());
modify.setAspmodifyassignedRole(role.getAssignedRoles());
modify.setAspmodifyassignedPermission(role.getAssignedPermissions());
modify.setCreatedby(role.getCreatedBy());
modify.setCreateddate(role.getCreatedDate());
modify.setUpdatedby(role.getUpdatedBy());
modify.setUpdateddate(role.getUpdatedDate());
modify.setUsername(role.getUserName());
modify.setModifyftid(role.getFtid());
System.out.println("assigned permissions==============>"+role.getAssignedPermissions());
System.out.println("updated by=================>"+role.getUpdatedBy());
System.out.println("update date=================>"+role.getUpdatedDate());
System.out.println("created by===================>"+role.getCreatedBy());
System.out.println("created date=====================>"+role.getCreatedDate());
System.out.println("ftid=============================>"+role.getFtid());
System.out.println("user name===========================>"+role.getUserName());
System.out.println("ftid=============>"+role.getFtid());
if(role!=null){
return SUCCESS;
}else{
return ERROR;
}
}
#Override
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
}
#Override
public ASPAdminModifyBean getModel() {
// TODO Auto-generated method stub
return modify;
}
}
Well, the code is grown again :)
Start by :
changing your ArrayList declarations to List (in both beans, and regenerate getters and setters);
changing the way you get the Request, the Response and the Session, by using Struts2 Aware s interfaces (and their setters, with code inside)
Then if it still doesn't work, describe a little better what do you mean by "values are not passing to Action", and consider posting your interceptor stack config.
Hope that helps...
Related
Help me please to remove the row in Postgres by clicking the Del button.
Maybe to link id in DB and id on the HTML page, but how?
Maybe I need to parse the HTML page and pass String id?
Any ideas will help me.
here is my database schema:
<table class="tmc">
<thead>
<tr>
<th>ID</th><th>TMC</th><th>SN</th><th>Owner</th>
</tr>
</thead>
<tbody>
{{#messages}}
<tr>
<td>{{id}}</td>
<td><span>{{text}}</span></td>
<td><i>{{sn}}</i></td>
<td><i>{{owner}}</i></td>
<th><form action="/remove" method="post">
<input type="submit" value="Del"/>
<input type="hidden" name="_csrf" value="{{_csrf.token}}" />
</form>
</th>
</tr>
{{/messages}}
</tbody>
#Entity
#Table(name = "message")
public class Message {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String text;
private String sn;
private String owner;
public Message() {
}
public Message(String text, String sn, String owner) {
this.text = text;
this.sn = sn;
this.owner = owner;
}
Rows are forming in database by:
#PostMapping("/main")
public String add (
#RequestParam String owner,
#RequestParam String text,
#RequestParam String sn, Map<String, Object> model) {
Message message = new Message (text, sn, owner);
if (text != null && !text.isEmpty() & sn != null && !sn.isEmpty() & owner != null &&
!owner.isEmpty()) {
if (!text.matches("^[0-9].*$")) {
messageRepo2.save(message);
Iterable<Message> messages = messageRepo2.findAll();
model.put("messages", messages);
} else
model.put("error", "ТМЦ не должно начинаться с цифры");
Iterable<Message> messages = messageRepo2.findAll();
model.put("messages", messages);
} else {
model.put("error", "Заполните все поля!");
Iterable<Message> messages = messageRepo2.findAll();
model.put("messages", messages);
}
return "main";
}
There are two issues with your html code:
it would be to let the form element outside the table
need to use <input type="hidden" name="xxx" value="{{xxx}}" /> in order to let it can pass parameter to your controller method
<form action="/remove" method="post">
<input type="hidden" name="_csrf" value="{{_csrf.token}}" />
<table class="tmc">
<thead>
<tr>
<th>ID</th><th>TMC</th><th>SN</th><th>Owner</th>
</tr>
</thead>
<tbody>
{{#messages}}
<tr>
<td>{{id}} <input type="hidden" name="id" value="{{id}}" /></td>
<td><span>{{text}}</span></td>
<td><i>{{sn}}</i> <input type="hidden" name="sn" value="{{sn}}" /></td>
<td><i>{{owner}}</i> <input type="hidden" name="owner" value="{{owner}}" /></td>
<td><input type="submit" value="Del"/></td>
</th>
</tr>
{{/messages}}
</tbody>
</table>
</form>
<thead>
<tr>
<th>ID ТМЦ</th><th>Наименование ТМЦ</th><th>Серийный номер ТМЦ</th><th>За кем числится</th>
</tr>
</thead>
<tbody>
{{#messages}}
<tr>
<td>{{id}}</td>
<td><span>{{text}}</span></td>
<td><i>{{sn}}</i></td>
<td><i>{{owner}}</i></td>
<td>
<form action="/remove" method="post" name="remove">
<input type="submit" value="Удалить"/>
<input type="hidden" name="_csrf" value="{{_csrf.token}}" />
<input type="hidden" name="sn" value="{{sn}}"/>
</form>
</td>
</tr>
{{/messages}}
</tbody>
import com.example.webapp.domain.Message;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
#Transactional
public interface MessageDel extends CrudRepository<Message, Long>{
List<Message> deleteBySn (String sn);
}
#PostMapping("/remove")
public String remove (#RequestParam String sn, Map<String, Object> model) {
Message messagedel = new Message (sn);
messageDel.deleteBySn(sn);
model.put("messages", messagedel);
return "redirect:/main";
}
<form:errors> tag not showing Error Messages when I put it inside the <form:form> tag of Spring.
It shows Error Messages, If the <form:errors> tag is out of the <form:form> tag. I have printed the errors of the binding result and it shows the errors as below:
[Field error in object 'student1' on field 'lastname': rejected value []; codes [Size.student1.lastname,Size.lastname,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [student1.lastname,lastname]; arguments []; default message [lastname],16,6]; default message [Size.student1.**lastname**]]
Note: If I place <form:errors> inside the <form:form> tag it is not working.
Here is the JSP code:
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Contact Manager</title>
<style>
.errStyle{
color:red;}
</style>
</head>
<body>
<h2>${headerName}</h2>
<!-- This line shows the error messages of input binding exception. -->
<!--<form:errors path="student1.*" cssClass="errStyle"/> this line shows the error messages-->
<form:form method="post" action="/SampleTutorials/student/addBean.html">
<label>Last Names</label>
<input type="text" name="lastname"/>
<table>
<tr>
<td><label>First Name</label></td>
<td><input type="text" name="firstname"/></td>
</tr>
<tr>
<td><label>Last Names</label></td>
<td><input type="text" name="lastname"/>
<form:errors path="student1.lastname"> </form:errors>
</td>
</tr><!--**** Here it doesnt shows the error message -->
<tr>
<td><label>DOB</label></td>
<td><input type="text" name="dob"/></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td><label>Telephone</label></td>
<td><input type="text" name="telephone"/></td>
</tr>
<tr>
<td><label>Skillset</label></td>
<td>
<select multiple name="skillSet">
<option value="J2EE">J2EE</option>
<option value="J2SE">J2SE</option>
<option value="Spring">Spring</option>
<option value="Hibernate">Hibernate</option>
</select>
</td>
</tr>
<tr>
<td><label>Flat Number : </label></td>
<td><input type="text" name="address.flatNumber"/></td>
</tr>
<tr>
<td><label>Building Name : </label></td>
<td><input type="text" name="address.buildingName"/></td>
</tr>
<tr>
<td><label>City : </label></td>
<td><input type="text" name="address.city"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Contact"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
The Controller Method:
#RequestMapping(value="/addBean.html", method = RequestMethod.POST)
public ModelAndView addContactFromBean(#ModelAttribute("student1") #Valid ContactBean student1, BindingResult result){
System.out.println("Inside addContactFromBean");
if(result.hasErrors()){ // this binding result checks for error
ModelAndView model = new ModelAndView("addStudent");
System.out.println("Some error occured in input.");
System.out.println(result.getAllErrors());
return model;
}
ModelAndView model = new ModelAndView("viewStudent");
System.out.println("Student Bean : "+student1.toString());
model.addObject("student1", student1);
System.out.println("View Name :->> "+model.getViewName());
return model;
}
The Spring Bean:
package com.springTut;
import java.util.List;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class ContactBean {
**#NotEmpty
private String firstname;**
**#Size( min=6, max=16, message="Size.student1.lastname")**
**private String lastname;**
private String email;
private String dob;
private Long telephone;
private List<String> skillSet;
private AddressBean address;
#Override
public String toString() {
return "ContactBean [firstname=" + firstname + ", lastname=" + lastname + ", email=" + email + ", telephone="
+ telephone + ", skillSet=" + skillSet + ", address=" + address + "--.]";
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public List<String> getSkillSet() {
return skillSet;
}
public void setSkillSet(List<String> skillSet) {
this.skillSet = skillSet;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getTelephone() {
return telephone;
}
public void setTelephone(Long telephone) {
this.telephone = telephone;
}
public AddressBean getAddress() {
return address;
}
public void setAddress(AddressBean address) {
this.address = address;
}
}
Binding and validation errors are registered in the model. The tag retrieves these errors from the model for display purposes. If the tag is nested inside a form:form tag the effective error key or path is the combination of the modelAttribute attribute in the form:form tag and the path you specify. In your case the modelAttribute is not explicit. Change your form as follows
<form:form method="post" action="/SampleTutorials/student/addBean.html" modelAttribute="student1">
<label>Last Names</label>
<input type="text" name="lastname"/>
<table>
<tr>
<td><label>First Name</label></td>
<td><input type="text" name="firstname"/></td>
</tr>
<tr>
<td><label>Last Names</label></td>
<td><input type="text" name="lastname"/>
<form:errors path="lastname"> </form:errors>
</td>
</tr>
<tr>
<td><label>DOB</label></td>
<td><input type="text" name="dob"/></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td><label>Telephone</label></td>
<td><input type="text" name="telephone"/></td>
</tr>
<tr>
<td><label>Skillset</label></td>
<td>
<select multiple name="skillSet">
<option value="J2EE">J2EE</option>
<option value="J2SE">J2SE</option>
<option value="Spring">Spring</option>
<option value="Hibernate">Hibernate</option>
</select>
</td>
</tr>
<tr>
<td><label>Flat Number : </label></td>
<td><input type="text" name="address.flatNumber"/></td>
</tr>
<tr>
<td><label>Building Name : </label></td>
<td><input type="text" name="address.buildingName"/></td>
</tr>
<tr>
<td><label>City : </label></td>
<td><input type="text" name="address.city"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Contact"/>
</td>
</tr>
</table>
</form:form>
Try to use the spring form properly for the model attribute object and the internal attributes i mean.
In your form
<form:form method="post" action="/SampleTutorials/student/addBean.html" attribute="student">
...
<td><form:input type="text" path="lastname"/>
<form:errors path="lastname"> </form:errors>
</td>
...
Let me know if in this case also fails
I need to download data from two database tables (factory and factoryType) using the form below but it isn't working. I managed to get data only out of one table... Can somebody take a look?
Factory table:factoryId,factoryname
FactoryType table: factoryType,factoryTypeId
FactoryConf table: factoryID,factoryTypeId
We are using Hibernate for the database operations.
Form
<form:form id="FactoryUploadForm" method="post" modelAttribute="FactoryUploadForm" action="/submitUploadFactoryForm" enctype="multipart/form-data">
<table class="ui-widget" width="100%>
<tr>
<td valign="top" colspan=3 height="40px">
<div class="column span-15 highlight">
</div>
</td>
<td>
<h>Click here to download file</h>
</td>
</tr>
<tr>
<td>
<div class="column span-2_5"><label for="filedata">Select CSV File:</label><span style="color: red">*</span></div>
</td>
<td align="left">
<div class="column span-4"><input class="span-5 ui-input" type="file" id="fileData" name="fileData" size="45" /></div>
</td>
</tr>
<tr>
<td>
<div class="column span-2_5"><label for="uploadComment">Comment/Description:</label></div>
</td>
<td align="left">
<div class="column span-4"><input class="span-5 ui-input" type="text" maxlength="100" id="uploadComment" name="uploadComment" size="50" /></div>
</td>
</tr>
<tr>
<td valign="top"> </td>
<td colspan=2>
<div class="column " style="margin-top: 15px;">
<input type="submit" value="Upload File" "/>
</div>
<div class="column" style="margin-top: 15px;">
<input type="button" value="Reset" onclick="javascript:resetUploadFactoryForm();"/>
</div>
</td>
</tr>
</table>
</form:form>
Controller:
#Controller
public class NewFactoryUploadDownloadController {
private static final Log logger = LogFactory.getLog(NewFactoryUploadDownloadController.class);
#Resource
IInteropService interopService;
#Resource
FactoryUploadRepository repository;
#RequestMapping(value="/download.do")
public String downloadFactory(FactoryUploadForm form, Model model,HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter pw = null;
try{
logger.debug("+++++++++++++++++++++++++++++++++++++++");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename="+ "Backup" +".csv");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-csheck=0, pre-check=0");
response.setHeader("Pragma", "public");
pw = response.getWriter();
// Get, Write and flush the data.
pw.println("Factory Id,FactoryLegalName,FactoryShortName,FactoryType");
List<Factory> factoryFromDB = Collections.emptyList();
factoryFromDB = Service.getfactories();
logger.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
for(Factory factory : factoryFromDB)
{
pw.println(String.format("%s,%s,%s",
factory.getId(),
formatString(factory.getName()),
factory.getShortName()));
}
pw.flush();
}catch (Exception ex){
logger.error("Error while downloading companies", ex);
model.addAttribute("failureMsg", "Error while downloading companies");
}
return null;
}
private String formatString(String str){
if (str.contains(",")){
return "\""+str + "\"";
}
return "\""+str + "\"";
}
}
Model:
#Entity
#Table(name = "FactoryConf", uniqueConstraints = {
#UniqueConstraint(columnNames = { "factoryId" } )
})
public class FactoryConf {
#Id
long factoryId;
#OneToOne
#JoinColumn(name = "factoryId", insertable = false, updatable = false)
Factory factory;
#ManyToOne(optional = false)
#JoinColumn(name = "factoryTypeId")
FactoryType factoryType;
public FactoryConf() {
super();
}
public FactoryConf(long factoryId, FactoryType factoryType) {
super();
this.factoryType = factoryType;
this.factoryId = factoryId;
}
public Factory getFactory() {
return factory;
}
public void setFactory(Factory factory) {
this.factory = factory;
}
public FactoryType getFactoryType() {
return factoryType;
}
public void setFactoryType(FactoryType factoryType) {
this.factoryType = factoryType;
}
public long getFactoryId() {
return factoryId;
}
public void setFactoryId(long factoryId) {
this.factoryId = factoryId;
}
public FactoryType getFactoryTypeByFactoryID(long factoryId){
return factoryType;
}
}
I have some trouble with my jsp page's date value. Basically I have a date picker which allows me to pick date and save it to database. If I do not pick a date, the value is null which I check before setting it and passing it to the database. If its null, I modify it to a dummy date and then save it to database. This part works fine. But if the jsp page throws the not null validation, the value goes to the setter as null, gets modified there to the dummy value and reflected back in the form. I don't want that dummy value to be visible to the user. So is there any way I can put a check on the jsp page so that if the value is a dummy date value it will be visible as null to the user?
<b>Order Information</b>
<table>
<tr>
<td align="left"><form:label for="orId" path="orId" cssErrorClass="error">* Order Id :</form:label></td>
<td align="left"><form:input path="orId" size="35" maxlength="35" /> <form:errors path="orId" /></td>
</tr>
<tr>
<td align="left"><form:label for="orServicetag" path="orServicetag" cssErrorClass="error">* Service tag:</form:label></td>
<td align="left"><form:input path="orServicetag" size="35" maxlength="35" value= "${ticketList.ticServicetag}"/> <form:errors path="orServicetag" /></td>
</tr>
<tr>
<td align="left"><form:label for="orOwnerSUUsername" path="orOwnerSUUsername" cssErrorClass="error">* SU UserName:</form:label></td>
<td align="left"><form:input path="orOwnerSUUsername" id="suusnm" size="35" maxlength="35" value= "${ticketList.ticOwnerSuUsername}"/> <form:errors path="orOwnerSUUsername" /></td>
</tr>
<tr>
<td align="left"><form:label for="orTicId" path="orTicId" cssErrorClass="error">* Ticket Id</form:label></td>
<td align="left"><form:input path="orTicId" id="ticcrdt" size="35" maxlength="35" value= "${ticketList.ticId}"/> <form:errors path="orTicId" /></td>
</tr>
<tr>
<td align="left"><form:label for="orTicCreatedDate" path="orTicCreatedDate" cssErrorClass="error">* Ticket Date (YYYY-MM-DD):</form:label></td>
<td align="left"><form:input path="orTicCreatedDate" size="35" maxlength="35" value= "${ticketList.ticDate}"/> <form:errors path="orTicCreatedDate" /></td>
</tr>
<tr>
<td align="left"><form:label for="orPartOrdered" path="orPartOrdered" cssErrorClass="error">* Part Name :</form:label></td>
<td align="left"><form:input path="orPartOrdered" size="35" maxlength="35" /> <form:errors path="orPartOrdered" /></td>
</tr>
<tr>
<td align="left"><form:label for="orOrderedDate" path="orOrderedDate" cssErrorClass="error">* Part Ordered Date (YYYY-MM-DD):</form:label></td>
<td align="left"><form:input path="orOrderedDate" id="datepicker1" size="35" maxlength="35" /> <form:errors path="orOrderedDate" /></td>
</tr>
<tr>
<td align="left"><form:label for="orRecievedDate" path="orRecievedDate" cssErrorClass="error"> Part Received Date (YYYY-MM-DD):</form:label></td>
<td align="left"><form:input path="orRecievedDate" id="datepicker2" size="35" maxlength="35" /> <form:errors path="orRecievedDate" /></td>
</tr>
<tr>
<td align="left"><form:label for="orStatus" path="orStatus" cssErrorClass="error">* Order Status:</form:label></td>
<td align="left">
<select name="orStatus" id = "orStatus" id="slectboxid1">
<option value = "">---Select---</option>
<option value="Open">Open</option>
<option value="Progress">Progress</option>
<option value="Part Ordered">Part Ordered</option>
<option value="Part Arrived">Part Arrived/ Received</option>
<option value="Waiting for Owner's computer">Waiting for Owner's computer</option>
<option value="Installation">Installation in progress</option>
<option value="Closed">Closed</option>
</select>
<form:errors path="orStatus" /></td>
</tr>
</table>
<div align="center" style="margin-top:15px;" >
<form action="OrderDatabase" method="get">
<input type="submit" name="submit" value="View Order"/>
<input type="submit" name="save" value="save"/> |
<input type="reset" name="reset"/>
</form>
</div>
</form:form>
The Code for the ticketDetailsBean is as follows:
package com.helplaw.beans;
//import java.sql.*;
import java.util.Date;
public class OrderDetailsBean {
private String orId;
private String orPartOrdered;
private Date orTicCreatedDate;
private Date orOrderedDate;
private Date orRecievedDate;
private String orOwnerSUUsername;
private String orServicetag;
private String orStatus;
private int orTicId;
public String getOrId() {
return orId;
}
public void setOrId(String orId) {
this.orId = orId;
}
public String getOrPartOrdered() {
return orPartOrdered;
}
public void setOrPartOrdered(String orPartOrdered) {
this.orPartOrdered = orPartOrdered;
}
public Date getOrTicCreatedDate() {
return orTicCreatedDate;
}
public void setOrTicCreatedDate(Date orTicCreatedDate) {
this.orTicCreatedDate = orTicCreatedDate;
}
public Date getOrOrderedDate() {
return orOrderedDate;
}
public void setOrOrderedDate(Date orOrderedDate) {
this.orOrderedDate = orOrderedDate;
}
public Date getOrRecievedDate() {
return orRecievedDate;
}
public void setOrRecievedDate(Date orRecievedDate) {
if(orRecievedDate == null){
System.out.println("d11:" + orRecievedDate);
this.orRecievedDate = new Date(000000001);
}
else if(orRecievedDate.toString().equals("1969-12-31")){
System.out.println("d12:" + orRecievedDate);
this.orRecievedDate = null;
}
else{
System.out.println("d13:" + orRecievedDate);
this.orRecievedDate = orRecievedDate;
}
}
public String getOrOwnerSUUsername() {
return orOwnerSUUsername;
}
public void setOrOwnerSUUsername(String orOwnerSUUsername) {
this.orOwnerSUUsername = orOwnerSUUsername;
}
public String getOrServicetag() {
return orServicetag;
}
public void setOrServicetag(String orServicetag) {
this.orServicetag = orServicetag;
}
public String getOrStatus() {
return orStatus;
}
public void setOrStatus(String orStatus) {
this.orStatus = orStatus;
}
public int getOrTicId() {
return orTicId;
}
public void setOrTicId(int orTicId) {
this.orTicId = orTicId;
}
}
Just remove the NULL validation because it will never be null in your case where you are always setting a dummy value.
Alternatively, you can achieve it using hidden input field.
Steps to follow:
create hidden input fields one for each date input field.
set the value in hidden input field if value is changed in actual date input field using JavaScript
now set the dummy value in hidden input field rather than actual input field if value is null
read values from hidden input fields to save it in database
Note: swap the path attribute date input field with hidden input fields so that it will be picked from hidden input fields when form is submitted.
i am quite new at this and i what to know how can i get a index row from a dynamical table created with struts2. Here is what i have tried so far:
test file
<table class="table table-bordered table-striped table-condensed table-hover"
id="exp">
<thead>
<tr>
<th class="center"></th>
<th>Name</th>
</tr>
</thead>
<tbody>
<s:if test="listanume.size>0">
<s:iterator value="listanume" status="statusVar">
<tr>
<td class="center">
<button class="btn" onclick="javascript:setval(0)">
delete
</button>
</td>
<td><s:property value="nume"/></td>
</tr>
</s:iterator>
</s:if>
</tbody>
</table>
<h4>Numarul randului este: <s:property value="mysample"/> </h4>
<script>
var mysample= '';
function= setval(varval){
mysample=varval;
alert(mysample);
}
</script>
pop java
public class Pop {
private int nrrow;
private List<UserModel> listanume = new ArrayList<UserModel>();
UserModel user = new UserModel(),
user1 = new UserModel(),
user2 = new UserModel();
private String[] sample;
{
sample = new String[]{"list1", "list2", "list3"};
}
public String execute() {
user.setEmail("eret#esre");
user.setPass1("1234");
user.setNume("gigel");
user1.setEmail("yuii#ihj");
user1.setPass1("6789");
user1.setNume("marius");
user2.setEmail("nmnn#cvx");
user2.setPass1("7676");
user2.setNume("sorin");
sample1.add(user);
sample1.add(user1);
sample1.add(user2);
return "succes";
}
public String afisnrow(){
execute();
listanume=sample1;
return "succes";
}
public String[] getSample() {
return sample;
}
public void setSample(String[] sample) {
this.sample = sample;
}
public List<UserModel> getSample1() {
return sample1;
}
public void setSample1(List<UserModel> sample1) {
this.sample1 = sample1;
}
public void setNrrow(int nrrow){
this.nrrow=nrrow;
}
public int getNrrow(){
return nrrow;
}
public List<UserModel> getListanume(){
return listanume;
}
public void setListanume(List<UserModel> listanume){
this.listanume=listanume;
}
}
every row has a button on it and when i click that button i need to get the row index, i kind of stuck and run out of ideas, any suggestions?
replace
onclick="javascript:setval(0)"
with
onclick="javascript:setval(<s:property value="#statusVar.index" />)"
more info on the struts2 iterator tag here.
The solution i have found is this:
test file
<table class="table table-bordered table-striped table-condensed table-hover"
id="exp">
<thead>
<tr>
<th class="center"></th>
<th>Name</th>
</tr>
</thead>
<tbody>
<s:if test="listanume.size>0">
<s:iterator value="listanume" status="statusVar">
<tr>
<td class="center">
<button class="btn" id="<s:property value='indexrow'/>">
delete
</button>
</td>
<td><s:property value="nume"/></td>
</tr>
</s:iterator>
</s:if>
</tbody>
</table>
<i><p id="test"> No button pressed.</p></i>
<script>
$("button").click(function(e){
var idClicked = e.target.id;
$("#test").html(idClicked);
});
</script>
pop java
public class Pop {
private int indexrow;
private List<UserModel> listanume = new ArrayList<UserModel>();
UserModel user = new UserModel(),
user1 = new UserModel(),
user2 = new UserModel();
public String execute() {
user.setEmail("eret#esre");
user.setPass1("1234");
user.setNume("gigel");
user1.setEmail("yuii#ihj");
user1.setPass1("6789");
user1.setNume("marius");
user2.setEmail("nmnn#cvx");
user2.setPass1("7676");
user2.setNume("sorin");
listanume.add(user);
listanume.add(user1);
listanume.add(user2);
return "succes";
}
public String afisnrow(){
execute();
for (UserModel userModel : listanume) {
UserModel userModel1=new UserModel();
indexrow++;
userModel1.setNume(userModel.getNume());
userModel1.setIndexrow(indexrow);
listanume.add(userModel1);
}
return "succes";
}
public List<UserModel> getListanume(){
return listanume;
}
public void setListanume(List<UserModel> listanume){
this.listanume=listanume;
}
}