How to check for an empty string in MyBatis? - mybatis

How do I check for an empty string in the dynamic SQL of MyBatis? I find the code below in the documentaiton, but I want to check for empty string, not null.
<select id="findActiveBlogWithTitleLike" parameterType="Blog" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
</select>

In MyBatis you can use != '' to compare with empty string, so in your query it would be something like:
<select id="findActiveBlogWithTitleLike" parameterType="Blog" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null and title != ''">
AND title like #{title}
</if>
</select>

Dont't speak English well. Thank you for your patient.
It's functions xml file.
<mapper namespace="org.jacknie.mybatis.Functions">
<sql id="isBlank">
<bind name="isBlank" value=":[#org.apache.commons.lang3.StringUtils#isBlank(#this)]" />
</sql>
<sql id="sysout">
<bind name="sysout" value=":[#System#out.println(#this)]" />
</sql>
</mapper>
It's mapper xml file.
<mapper namespace="org.jacknie.test.TestMapper">
<select id="selectTest" resultType="_int">
<include refid="org.jacknie.mybatis.Functions.isBlank" />
<include refid="org.jacknie.mybatis.Functions.sysout" />
SELECT '1' FROM DUAL
<if test="#fn = isBlank, not(#fn(map.name))">
<bind name="forLogging" value="#fn = sysout, #fn('Hello' + map.name)" />
</if>
</select>
</mapper>
How about think this tip...
enter link description here

Related

How to add multiselect in form

I am newbie in SalesForce Commerce Cloud. So if my question is silly then I am sorry in advance.
My Target is to show the multiselect on customer registration form. By searching on internet what I have done
Step 1: Add form definition
<field formid="brands" label="label.select.interestedbrands.preferences" type="integer" mandatory="true" binding="profile.interestedBrands">
<options>
<option optionid="1" label="Brand 1" value="1"/>
<option optionid="2" label="Brand 2" value="2" />
<option optionid="3" label="Brand 3" value="3" />
<option optionid="4" label="Brand 4" value="4" />
<option optionid="5" label="Brand 5" value="5" />
<option optionid="6" label="Brand 6" value="6" />
<option optionid="7" label="Brand 7" value="7" />
</options>
</field>
Step 2: In isml template file I have rendered it like this
<select class="custom-select form-control" id="brands" <isprint value="${pdict.preferencesForm.brands.attributes}" encoding="off" /> multiple>
<isloop items=${pdict.preferencesForm.brands.options} var="brand">
<option id="${brand.id}" value="${brand.htmlValue}" <isif condition="${brand.selected}">selected</isif> >${brand.label}</option>
</isloop>
</select>
To save it in the system object profile I have added one new attribute which type is Enum of Integer and multiselect true.
Problem: Now when I try to save the form in the controller it have only one value whether I select multiple options or not.
Can you please let me know how can I add the multi-select in the SFCC and which mistake I am doing?

Coldfusion: specific action after selecting an option in select

I have following code:
<cfif session.language is ("DE")>
<cfset bl=ValueList(getContent.G,",")>
<cfelseif session.language is ("FR")>
<cfset bl=ValueList(getContent.H,",")>
<cfelseif session.language is ("EN")>
<cfset bl=ValueList(getContent.I,",")>
</cfif>
<cfset tags = sizes />
<cfset bltags = bl />
<cfset tagArray = arrayNew(1) />
<cfset tagArrayDATA = arrayNew(1) />
<cfloop list="#tags#" index="tag" delimiters=",">
<cfif not ArrayFindNoCase(tagArray,tag)>
<cfset arrayAppend(tagArray, tag) />
</cfif>
</cfloop>
<cfloop list="#bltags#" index="tag" delimiters=",">
<cfif not ArrayFindNoCase(tagArrayDATA,tag)>
<cfset arrayAppend(tagArrayDATA, tag) />
</cfif>
</cfloop>
<cfoutput>
<cfif isdefined("tagArray") AND arraylen(tagArray) GT 1>
<form name="frmsize" id="frmsize" action="/index.cfm?showusage" method="post">
<cfif isdefined("tagArray") AND arraylen(tagArray) GT 1>
<div>
<select name="valuesize">
<option value="">Choose your option</option>
<cfloop from="1" to="#arraylen(tagArray)#" index="i">
<option value="#tagArray[i]#">#tagArray[i]#
<cftry>
#tagArrayDATA[i]#
<cfcatch>
</cfcatch>
</cftry>
</option>
</cfloop>
</select>
</div>
</cfif>
</form>
</cfif>
</cfoutput>
My goal is to send a value from tagArrayDATA[i] via link.
It should look like that:
<form name="frmsize" id="frmsize" action="/index.cfm?showusage&valueArrayData="#tagArrayDATA[i]#" method="post">
I don't know how to manage that because the cfloop is below the action attribute of the form.
You can't really do what you are trying to do with server side code alone as far as I can tell. But you have a couple of options. One easy one is to just Javascript to update the action when the select is changed.
The other option is to put both values in the select and parse on the end.
<cfloop from="1" to="#arraylen(tagArray)#" index="i">
<option value="#tagArray[i]#-#tagArrayDATA[i]#">
#tagArray[i]# #tagArrayDATA[i]#
</option>
</cfloop>
Then when you are parsing the data just do:
<cfset data = listToArray(FORM.valuesize,'-') />
<!-- data[1] will be the selected value of #tagArray[i]# -->
<!-- data[2] will be the selected value of #tagArrayData[i]# -->
<!-- This assumes the - will never be actually in the data, you could use a different separator -->
My guess is they are both strings and this should work, though I have no idea why you have a try/catch in the select part of your code, probably look at a better way of doing that. If you really need that, I would clean it up as.
<cfloop from="1" to="#arraylen(tagArray)#" index="i">
<cfset data = '' />
<cftry>
<cfset data = tagArrayData[i] />
<cfcatch></cfcatch>
</cftry>
<option value="#tagArray[i]#-#data#">
#tagArray[i]# #data#
</option>
</cfloop>
Though if you are processing the data on the other end, I would make sure all the data is either in the FORM or the URL scopes but not mix. I would be pissed to have to parse some form data in the FORM scope and other data in the URL scope.

Is there a way to test the existence of a parameter in the parameter map?

Is there a way in mybatis to test if a certain parameter is contained in the query parameter map?
I'm looking for something like this:
<select id="selectByKey" resultMap="MyObjectMap" parameterType="map">
select obj.* from obj where obj.id=#{id:VARCHAR}
<!-- I'm looking for a method like that below -->
<if test="parameterExists('forUpdate')">
<if test="forUpdate == 1">
for update
</if>
</if>
</select>
You can use this test:
<if test="_parameter.containsKey('forUpdate')">your code....</if>
<if test="forUpdate != null">
may work.
In java HashMap.get(key) return null when key doesn't exist in map.

how to populate a drop down(<s:select) in struts2 from json response

i am unable to populate my Struts2 select drop down in my jsp page.
Note: My jsp page loads through json response.
I have a select box inside a form. But i don't t know how to populate it when my jsp page loads.
And in the same page i have a select dropdown in my Struts2 jquery grid,which i am able to populage but i don't know how to populate a drop down for my form which are out side of the grid.
Please help me regarding this issue.
my jsp page
<s:url id="selecturl" action="selectaction"/>
<s:url id="bookediturl" action="bookeditt"/>
<s:url id="bookremoteurl" action="booksetups"/>
<sjg:grid
id="bookgrid"
formIds="form2"
reloadTopics="reloadMyGrid"
caption="List of Cover Details"
dataType="json"
href="%{bookremoteurl}"
pager="true"
gridModel="gridModel"
rowList="50,100,5000"
rowNum="20"
filter="true"
filterOptions="{stringResult :false,searchOnEnter : false,enableClear : true,gridModel : true}"
rownumbers="true"
editurl="%{bookediturl}"
navigator="true"
editinline="true"
navigatorSearch="false"
autowidth="false"
width= "1550"
viewrecords="true"
navigatorRefresh="true"
navigatorDelete="true">
<sjg:gridColumn name="id" index="locationId" title="ID" formatter="integer" sortable="false" key="true"
search="false" editable="true" hidden="true" />
<sjg:gridColumn name="authLastname" index="authLastname" title="AuthorLastname" sortable="true" search="true" editrules="{required: true}"
editable="true" edittype="text" />
<sjg:gridColumn name="subjectId" title="Subject Name" editable="true" sortable="false" align="center" search="false" searchtype="select"
searchoptions="{dataUrl:'%{selecturl}'}" edittype="select" editoptions="{dataUrl:'%{selecturl}'}" />
</sjg:grid>
<div id="myDivBox" style="display:none; width :300px;position: relative;top: -30px;left: 299px;button:0px;">
<s:form action="searchkeywordss" id="form2" theme="simple" cssClass="yform">
<sj:textfield placeholder="Enter keyword to search" cssClass="txtbox" size="42" maxLength="255" name="keywordsearch"/>
<sj:select href="%{selecturl}" name="countryselect" list="%{lstsub}" listKey="id" listValue="subjectName" theme="jquery" cssStyle="margin-top:4px;"/>
<sj:a button="true" id="btnid" cssStyle="position: relative;top: 0px;left: 09px;" onClickTopics="reloadMyGrid">Find</sj:a>
</s:form>
</div>
struts.xml
<action name="booksetups" class="v.esoft.actions.bookdetails.BookdetailsAction" >
<result name="success" type="json"/>
<result name="login" type="redirect"> /index.jsp </result>
</action>
<action name="selectaction" class="v.esoft.actions.bookdetails.SelectAction">
<result name="success">/select.jsp</result>
</action>
You should only use the sj:select like below
<sj:select href="%{selecturl}" name="countryselect" list="subjectList" listKey="id" listValue="subjectName" theme="jquery" cssStyle="margin-top:4px;"/>
where selecturl returns a json response, subjectList is name of list in the json reponse which contains the options
Your selectaction should have a json response, but what you have now is an action returning a jsp so it would not work. If you cannot change it, you should create another action which returns json.

Use <form:select> tag with a map

Is there a way to map the data inside a map to tag?
I have a map Map<String, Integer> in my code.
Is there a way to map the option labels to the String in the map and the Integer to the option values?
The <form:options> tag supports what you want right out of the box, using the items attribute. You can do something like this:
LinkedHashMap<Integer, String> states = new LinkedHashMap<Integer, String>();
states.put(1, "Alabama");
states.put(2, "Alaska");
states.put(3, "Arizona");
states.put(4, "Arkansas");
states.put(5, "California");
And so on. Then in your form:
<form:select path="state">
<form:options items="${states}" />
</form:select>
That will be rendered to something like:
<select name="state">
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option>
<option value="5">California</option>
</select>
See the Spring form:select and form:options documentation. Use items, itemValue, and itemLabel as needed.
<form:select path="myFormVariable">
<form:option value="0" label="Select One" />
<form:options items="${myCollection}" itemValue="propertyToUseAsValue" itemLabel="propertyToUseAsDisplay" />
</form:select>