I have a problem using Struts1 ActionForm beans. Please, see a part of my struts-config:
<!-- RuleSearchForm is a sublass of RuleForm -->
form-beans>
<form-bean name="ruleForm"
type="forms.RuleForm">
</form-bean>
<form-bean name="ruleSearchForm"
type="forms.RuleSearchForm">
</form-bean>
</form-beans>
<!-- Mappings -->
<action path="/RuleList"
type="actions.RuleList"
name="ruleSearchForm"
scope="session"
validate="false">
<forward name="success" path="/html/view/RuleList.jsp"></forward>
</action>
<action path="/RuleCreate"
type="actions.RuleCreate"
name="ruleForm"
scope="request"
validate="false">
<forward name="success" path="/html/view/CreateUpdateRule.jsp"></forward>
</action>
And parts of my Actionform beans code:
public class RuleForm extends ActionForm {
protected Integer crid;
protected List levels;
/** Some other fileds go here */
public Collection getLevels(){
if(levels == null){
levels = DAOClass.getLevels();
Collections.reverse(levels);
}
return levels;
}
/** Other getters/setters go here */
}
public class RuleSearchForm extends RuleForm{
/**
* Avoid filter reset. If needs to be reset use {#link RuleForm#resetBeanFields()} directly.
* */
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
/**
* Add empty value. User should have an opportunity not to set value for this field.
* */
public Collection getLevels(){
if(levels == null || levels.size() == 0){
super.getLevels();
levels.add(0, new Level());
}
return levels;
}
}
The problem is:
User goes to /RuleList.do and sees the list of rules. ruleSearchForm used as a bean for transferring search params to /RulesList.do action. Initially tihs bean is empty, only getLevels() returns "empty value" + list of levels got from superclass method.
User goes to /CreateRule.do, ruleForm is used for collecting user input. levels property is used in selectbox. And I get there list of levels + empty row. This empty row is not added in RuleForm (named ruleForm). It's added it subclass of RuleForm. Why does super class ActionForm bean with NO static fields, with other name gets values from it's sublass instance???
If users saves Rule and has been redirected to /RuleList.do, the he sees populated (i.e. filled) search form ("ruleSearchForm") with values from "ruleForm".
What does it mean? Please help, I don't understand this mess of data beetween ActionForm Beans
UPD:
Now, I've changed inheritance of FormAction beans. I've introduced BaseFormBean. This BaseFormBean got two children: RuleForm and RuleSearchForm.
It didn't help. Still attributes from one bean are moved to another.
My jsp code:
CreateUpdateRule.jsp:
<html:form action="/RuleSave.do">
<html:hidden property="crid"/>
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td><bean:message key="rule.levelId"/></td>
<td><html:select property="levelId">
<html:optionsCollection property="levels" value="clid" label="name" />
</html:select>
</tr>
<tr>
<td><bean:message key="rule.timeStart"/></td>
<td><html:text property="timeStartStr"/></td>
</tr>
<tr>
<td><bean:message key="rule.timeEnd"/></td>
<td><html:text property="timeEndStr"/></td>
</tr>
<tr>
<td>
<html:submit styleClass="wpsButtonText"><bean:message key="application.submit"/></html:submit>
</td>
<td>
<input type="button" onclick="cancelOperation()" class="wpsButtonText" value="<bean:message key="application.cancel"/>" />
<html:link styleClass="cancelLink" page="/RuleList.do"></html:link>
</td>
</tr>
</table
</html:form>
my RuleList.jsp:
<html:form action="/CritRuleList.do" >
<table style="width: 100%;">
<tr>
<td><bean:message key="rule.levelId"/></td>
<td><html:select property=levelId">
<html:optionsCollection property="levels" value="clid" label="name" />
</html:select>
</td>
</tr>
<tr>
<td><bean:message key="rule.timeStart"/></td>
<td><html:text property="timeStartStr" /></td>
</tr>
<tr>
<td><bean:message key="rule.timeEnd"/></td>
<td><html:text property="timeEndStr" /></td>
</tr>
<tr>
<td colspan="2">
<html:submit styleClass="wpsButtonText"><bean:message key="application.search"/></html:submit>
<input type="button" onclick="cancelOperation(this)" class="wpsButtonText" value="<bean:message key="critrule.searchClear"/>" />
<html:link styleClass="cancelLink" page="/RuleResetSearchFilter.do"></html:link>
</td>
</tr>
</table>
</html:form>
It sounds very strange that a subclass method would be called in the case you've described. To find out why it's happening, you need to debug your code - put a breakpoint inside the if construct of the RuleSearchForm.getLevels() method and see if it is really called (and where did the call originate from).
Other than that, you could try to move the populating logic of levels away from the form(s) altogether, and instead do it in the action. So, in the RuleCreate action you would do something like this:
List levels = DAOClass.getLevels();
Collections.reverse(levels);
request.setAttribute("levels", levels);
This code is really hard to follow, and you still left out or changed some mappings (RuleCreate or CreateRule). My guess would be that you have a misunderstanding in which form declaration is used to populate the fields and the options collection. When you create a form like this
<html:form action="/RuleSave.do">
The values for the select box are taken from the form bean associated with the RuleSave action in your struts-config, not from the form of the action that forwarded to this jsp.
From http://struts.apache.org/1.2.x/userGuide/struts-html.html#form:
Renders an HTML element ... The form bean is located, and created if necessary, based on the form bean specification for the associated ActionMapping.
The [roblem has been resolved.
1. As Tommi said, I've inspected DAO code. DAO returns list of beans kept in static private field.
/**
* Add empty value. User should have an opportunity not to set value for this field.
* */
public Collection getLevels(){
if(levels == null || levels.size() == 0){
super.getLevels();
levels.add(0, new Level());
}
return levels;
}
So each time I add empty bean I do change that static list of objects. Terrible bug.
Params transferring. I've created struts1 app for WebSphere Portal. Portal doesn't have redirect, because I work with portlets, not with pages. That is why request parameters live in request unlit last jsp page is rendered. It has been also solved.
Related
I need to perform validation against the table that is being displayed like this.
code sample how I'm trying to find the checkbox Element
//navigate t the second tr where the input and img tags are stored
List<WebElement> daysCheckBox = this.driver.findElements(By.xpath(".//*[#id='RULE_KEY']/div/div/div/div/div/center[1]/table[2]/tbody/tr[2]/td"));
System.out.println("Test checkbox");
for(WebElement td : daysCheckBox){
System.out.println(td.getTagName());
}
The problems I have is that this table come with 2 tags first row represents the days and the second "checkboxes" as you can see they line up with days, however checkbox have no link to each day. I have tried to solve this with reverse logic to try to identify the input tag if it is disabled. But when the box is selected it turns from input tag into img tag, also when I identify input tags I cannot pinpoint what day deselected box it corresponds to.
Does anyone have any advice or suggestion how to approach this type of validation?
Thank you.
I have put the source to make it clearer how the picture above is presented.
<tbody>
<tr bgcolor="whitesmoke">
<td><b>Sun</b></td>
<td><b>Mon</b></td>
<td><b>Tues</b></td>
<td><b>Wed</b></td>
<td><b>Thurs</b></td>
<td><b>Fri</b></td>
<td><b>Sat</b></td>
</tr>
<tr>
<td><img src="webwb/dhtmlx_iconcheckall.gif" /></td>
<td><input disabled="disabled" type="checkbox" /></td>
<td><input disabled="disabled" type="checkbox" /></td>
<td><input disabled="disabled" type="checkbox" /></td>
<td><img src="webwb/dhtmlx_iconcheckall.gif" /></td>
<td><img src="webwb/dhtmlx_iconcheckall.gif" /></td>
<td><input disabled="disabled" type="checkbox" /></td>
</tr>
</tbody>
You can create method which will convert Your table to Java Map for example:
public static Map<String,String> getTableAsMap(WebDriver driver)
{
Map<String,String> checkboxMap = new TreeMap<>();
List<WebElement> header = driver.findElements(By.xpath("((//tbody/tr)[1])/td/*"));
List<WebElement> checkboxes = driver.findElements(By.xpath("((//tbody/tr)[2])/td/*"));
for(int i = 0;i<header.size();i++){
//Only for testing purpose
System.out.printf("KEY: %s, VALUE: %s\n",header.get(i).getText(),checkboxes.get(i).getAttribute("disabled"));
checkboxMap.put(
header.get(i).getText(),
checkboxes.get(i).getAttribute("disabled")
);
}
return checkboxMap;
}
Then In your main class with driver use:
Webdriver driver = new FirefoxDriver();
Map<String,String> map = getTableAsMap(driver);
System.out.println(map.get("Fri"));
System.out.println(map.get("Sat"));
For disabled (non selected) checkbox You will receive true for selected there will be null. Both as String.
as you haven't posted the code, my answer also doesn't have a code :)
Create the WebElement for second row, i.e. xpath: //tbody/tr[2]
Now traverse all the tds inside this row and check their child element.
If the child element is input you know that checkbox is not selected else it is.
About for which day, you know the first is Sunday and Second is Monday and so on..
The Page Object Model approach may be effective to deal with this scenario. So, you can refresh your page object after some action/set of actions.
Since you mentioned that once a checkbox is checked the tag gets changed for the element, so I assume this can happen multiple times in the page based on user activity.
I can suggest that you implement a callback that reloads the and set the page objects on user action say click. With the page objects, you always have track of all the WebElements, accessibility and any change of state.
I have a block of code that uses the Html.BeginForm to submit a value from textbox to controller. When I put this in a View, it works fine. That is, the controller's action method is called. However, when I place this block of code inside a partial view that will get rendered in the View, the controller's action never gets called.
Not sure if this is the usual behavior or if I am missing something...
#using (Html.BeginForm("TestAction", "Home", FormMethod.Post, new { id = "formId" }))
{<table>
<tr>
<td>Data Date</td>
<td>#Html.TextBox("date")</td>
</tr>
<tr>
<td></td>
<td><input id="btnRun" type="submit" value="submit" /></td>
</tr>
}
Controller:
[HttpPost]
public ActionResult TestAction(string date)
{
[doing something......]
return View();
}
Thanks in advance!
Typically #Html.BeginForm() renders the <form> tag's action to point back to the route details that got it here. Thus in partial views, you're actually rendering back to the outer page. (Yeah, mind-bending.) If you want to specifically direct the form post to a particular route, add additional parameters to the #Html.BeginForm(). See http://msdn.microsoft.com/en-us/library/system.web.mvc.html.formextensions.beginform(v=vs.108).aspx
I have the following partial view binded to a view model.
#model Omega.UI.WebMvc.Models.InputWithLabelViewModelBase
#{
string required = null;
}
#if (Model.IsRequired)
{
{
required = "required";
}
}
<td><label id="#Model.LabelId">#Model.LabelText</label></td>
<td>
<input id="#Model.InputId" data-bind= "value: #Model.CurrentValueProperty" required="#required"/>
<span class="k-invalid-msg" data-for="#Model.InputId"></span>
</td>
When IsRequired is true I want the required attribute to be available, otherwise not. When I run that code and IsRequired is false the required attribute is presented with empty value: required="". I am quite new to asp mvc and I do not see what am I doing wrong.
UPDATE per dove`s post:
This does not work:
#{
bool required = Model.IsRequired;
}
<td><label id="#Model.LabelId">#Model.LabelText</label></td>
<td>
<input id="#Model.InputId" data-bind= "value: #Model.CurrentValueProperty" required="#required" />
<span class="k-invalid-msg" data-for="#Model.InputId"></span>
</td>
This renders the following html:
required="False" (if IsRequired is false and vice versa)
which is incorrect:
from the html documentation:
The required attribute is a boolean attribute, and can be set in the following ways:
<input required>
<input required="required">
<input required="">
Note: I am using Razor 2.0
Am I doing something wrong?
Mvc takes works with booleans in these special cases (assuming required works like checked). Try this:
required="#Model.IsRequired"
Can you also check your project references to confirm you are using Razor 2 as your example above should work also. Right click on System.Web.Razor view the properties and see what Version it is. It should be 2.0.0.0
I need to apply a different class to a <tr> according to the $index of the repeat directive. For example
<table>
<tr ng-repeat="model in list" class="xxx">
<td>...</td>
<tr>
</table>
I need to apply a different style to <tr> depending on whether the index is even and odd.
How could I solve this?
Normally you would use ngClassOdd (http://docs.angularjs.org/api/ng.directive:ngClassOdd) and ngClassEven (http://docs.angularjs.org/api/ng.directive:ngClassEven) directives like this:
<tr ng-repeat="item in items" ng-class-odd="'class1'" ng-class-even="'class2'">
Here is the jsFiddle: http://jsfiddle.net/pkozlowski_opensource/hNHJ4/1/
Unfortunately there is an issue where the mentioned directives are not working correctly when rows are removed: https://github.com/angular/angular.js/issues/1076
As a work around you can use the ngClass directive (http://docs.angularjs.org/api/ng.directive:ngClass) with the $index variable exposed by a repeater:
<tr ng-repeat="item in items" ng-class="{class1 : $index%2==0, class2 : !($index%2==0)}">
Here is the jsFiddle: http://jsfiddle.net/pkozlowski_opensource/am7xs/
It is not super-clean, but could be improved (for example, by exposing a function in a root scope, something like:
ng-class="getClass($index,'class1','class2')"
till the mentioned bug is fixed
If this is just for styling you can just use CSS
tr:nth-child(odd) { ... }
tr:nth-child(even) {...}
see http://jsfiddle.net/5MSDC/ for an example
I have a list that must be filled with a bit of code behind (prepopulating some fields, and do a bit of work on the save button.)
What is the best way to do that ?
thx
Edit: I ended by creating a custom webpart on the default.aspx. In this web part I have a bunch of :
<table border="0" cellspacing="0" width="100%">
<tr>
<td width="190px" valign="top" class="ms-formlabel">
<h3 class="ms-standardheader">
<nobr>Title<span class="ms-formvalidation"> *</span>
</nobr>
</h3>
</td>
<td width="400px" valign="top" class="ms-formbody">
<SharePoint:FormField runat="server" id="fldTitle" ControlMode="New" FieldName="Title" ListId="{MyListID}" />
<SharePoint:FieldDescription runat="server" id="ff1description" FieldName="Title" ControlMode="New" ListId="{MyListID}"/>
</td>
</tr>
This is working, but I found this in a bit painfull because I have to read each form field in code behind :
private void Set(SPListItem item, string fieldInternalName, object fieldValue)
{
var field = item.Fields.GetFieldByInternalName(fieldInternalName);
item[fieldInternalName] = fieldValue;
}
protected void Btn_Ok_Click(object sender, EventArgs e)
{
SPWeb thisWeb = SPContext.Current.Web;
SPList myList= thisWeb.Lists["mylist"];
SPListItem newItem;
newItem= myList.Items.Add();
var router = thisWeb.EnsureUser(#"myuser");
Set(newItem, "Title", fldTitle.Value);
Set(newItem, "OtherField", fldOther.Value);
Set(newItem, "AnotherField", GetFromBusinessLogic());
SPUtility.Redirect(thisWeb.Url, SPRedirectFlags.Default, System.Web.HttpContext.Current);
newItem.Update();
}
Is there any way to wrap all of this in a custom form container ? Maybe a custom ListFormWebPart with inner templates and code-behind events ?
You can follow the suggestion here to add in the code-behind.
BUT even if you put in a code-behind file, you still cannot reference the controls (fields, save buttons etc) on the form from the code-behind.
I would suggest writing up a web service (asmx or WCF) and host it on the Sharepoint instance.
You can then use javascript and AJAX calls to perform initialization and validation.