Conditional rendering in Spring in Action 4 - spring

I've been working on an example on conditional rendering from Spring in Action 4.
The code looks this way:
<sec:authorize access="hasRole('ROLE_SPITTER')">
<s:url value="/spittles" var="spittle_url" />
<sf:form modelAttribute="spittle" action="${spittle_url}">`
<sf:label path="text"><s:message code="label.spittle" text="Enter spittle:"/> </sf:label>
<sf:textarea path="text" rows="2" cols="40" />
<sf:errors path="text" />
<br/>
<div class="spitItSubmitIt">
<input type="submit" value="Spit it!" class="status-btn round-btn disabled" />
</div>
</sf:form>
</sec:authorize>
The question is what does the code attribute, namely label.spittle refers to? Does it refer to the modelAttribute from the form? Or does it mean that there should be message bundle with key label.spittle?

It means a key in a resource bundle (documentation):
<s:message code="label.spittle" text="Enter spittle:"/>

Related

bootstrap multiselect box not displayed correctly

Im trynig to create a simple multiselect box but for some reason its not visible properly.
Here is my code:-
HTML
<input type="text" id="addRow" />
<input type="button" id="btn" value="Add" />
<form id="form1">
<div style="padding:20px">
<select id="chkveg" multiple="multiple"></select>
<br />
<br />
</div>
</form>
https://jsfiddle.net/04Lgnkqs/
After creating the select you should call the plugin on your select. something like this
$('#chkveg').multiselect();
Refer here

Ajax validation on form submit

I am new in jsf using 2.2.12 version.
I have some problem in performing actions
1). I am getting a list of things and I need to show that list in as autocomplete. I did enough google and I found this things is for primefaces not able to find some helpful in jsf.
2). I have form with multiple fields(input box,password and checkbox). I need to validate some form fields like password and conform password matching and email structure etc. I am able to perform validation by custom validator. Now my requirement is if validation fails on form fields then ajax should not be submitted by clicking on submit button of form. The form is submitted by ajax request also.
Below is my form code.
<div class="email-signup" id="email-signup">
<h:form id="signupForm">
<div class="form-group">
<h:inputText value="#{userRegister.userName}" placeholder="username" class="input-text full-width" />
</div>
<div class="form-group">
<h:inputText value="#{userRegister.firstName}" placeholder="first name" class="input-text full-width" />
</div>
<div class="form-group">
<h:inputText placeholder="last name" value="#{userRegister.lastName}" class="input-text full-width" />
</div>
<div class="form-group">
<h:inputText id="email" value="#{userRegister.email}" validatorMessage="Wrong formatted email entered" class="input-text full-width" placeholder="email address">
<!-- <f:validator validatorId="emailValidator"/> -->
<f:validateRegex pattern="^\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,3}$" />
<f:ajax event="blur" render="emailMessage" />
</h:inputText>
<h:message id="emailMessage" for="email" style="color:red"/>
</div>
<div class="form-group">
<h:inputSecret id="password" value="#{userRegister.password}" class="input-text full-width" placeholder="password">
</h:inputSecret>
</div>
<!--<h:outputLabel for="confirm" value="Confirm password" />-->
<div class="form-group">
<h:inputSecret id="confirm" binding="#{confirm}" class="input-text full-width" placeholder="confirm password"/>
</div>
<div class="form-group">
<div class="checkbox">
<h:outputLabel>
<h:selectBooleanCheckbox/> Tell me about Chameleon news
</h:outputLabel>
</div>
</div>
<div class="form-group">
<p class="description">By signing up, I agree to Chameleon's Terms of Service, Privacy Policy, Guest Refund Policy, and Host Guarantee Terms.</p>
</div>
<h:commandButton value="SIGNUP" class="full-width btn-medium">
<f:ajax execute="signupForm" render="outputMessage" valueChangeListener="#{userRegister.getCallUserRegisterService}" onevent="showSignUpMessage" />
<h:outputText class="signup_message" id="outputMessage" value="#{userRegister.message !=null ? userRegister.message : ''}"/>
</h:commandButton>
</h:form>
</div>
Please have a look on above problems.
First of all I do not recommend to use JSF as frontend framework with ajax. We have project with JSF2.2 + primefaces and we are dealing with a lot of JSF lacks. JSF wasn't designed for ajax and it have more and more problems when you are trying to create a single page application with it.
If you have to use jsf - you must now that components libraries are your friends :). For autocomplete you can use eg:
primefaces autocomplete
rich faces
ice faces
You can of course write your own component but you already have in opensource libraries - I recommend PrimeFaces.
Your second problem
The best way is to use already existing command button in primefaces which has attribute ajax which defines if your action should be invoked by ajax or by standard request.
<p:commandButton value="Non-Ajax Submit" id="nonAjax" actionListener="#{buttonView.buttonAction}" ajax="true" />
If set ajax to true then your action will be performed by ajax request and if validation fails in the response you will get validation errors. You have also notice that command button have attribute "update" which should contain list of elements which should be updated after action proceed eg
<p:commandButton value="Non-Ajax Submit" id="nonAjax" actionListener="#{buttonView.buttonAction}" ajax="true" update="#form"/>
after this action whole form will be updated

how to call two submit buttons in spring form, one calls j_spring_security_check and another one calling controller method

How to call two submit buttons in spring form,
one button calls j_spring_security_check and another button one calling controller method.
following is login form
<form action="../../j_spring_security_check" method="post" >
<div class="right">
<ul>
<li>
<spring:message code="label.userName" text="UserName" />
<input id="j_username" name="j_username" type="text" />
<form:errors path="firstName" cssclass="error"> </form:errors>
</li>
<li>
<spring:message code="label.password" text="Password" />
<input id="j_password" name="j_password" type="password" />
<form:errors path="password" cssclass="error"></form:errors>
</li>
<li>
<input type="submit" value="Login" />
</li>
</ul>
</div>
</form>
<form action="/portal/main/resetForgetPassword" method="POST">
<input type="submit" value="ForgetPassword">
</form>
thanks advance
I guess your problem is that for every URL it is going to Security form of spring.
So you need to permit for some url so that it should not go to j_spring_security_check
You can do this in the security.xml (whatever may be the name of that file).
<intercept-url pattern="/portal/main/forgetPassword*" access="permitAll" />
<intercept-url pattern="/*" access="hasRole('ROLE_ADMIN_ASSUMED')">
And also in your html code
Forget Password

Why 2 Ajax call are made by a4j

With a simple form when i submit I see 2 request being made for this.
<h:form id="form1">
<a4j:commandButton value="Send" id="cm1" onclick="sendData(193)"/>
<a4j:jsFunction name="sendData">
<a4j:param name="param1" assignTo="#{outageManagementAction.deleteEngineId}" />
</a4j:jsFunction>
</h:form>
The code generated by JSF is following...
<form id="form1" name="form1" method="post" action="/moutagev1/faces/pages/xOutageEdit.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="form1" value="form1" />
<input id="form1:cm1" name="form1:cm1" onclick="jsf.util.chain(this,event,"sendData(193)","RichFaces.ajax(\"form1:cm1\",event,{\"incId\":\"1\"} )");return false;" value="Send" type="submit" /><span id="form1:j_idt257" style="display: none;"><script type="text/javascript">sendData=function(param1){RichFaces.ajax("form1:j_idt257",null,{"parameters":{"param1":param1} ,"incId":"1"} )};</script></span><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="8425501786648002013:2772799535475824519" autocomplete="off" />
</form>
Any Idea why this is happening...
Well, one request is the standard post of the <h:form> the second one is the custom <a4j:jsFunction> which is chained together with the submit when the click happens.
One solution would be to suppress the standard submit by changing the type of the button like this: <a4j:commandButton type="button" />.
Another alternative would be to not use <a4j:jsFunction> at all. Instead use this:
<f:setPropertyActionListener target="#{outageManagementAction.deleteEngineId}" value="193" />

Embed Spring Security login form to an existing page

How do I add the spring security login form to an existing page?
For example, let's say I have the following test.jsp page (which is not a spring login form page):
<html>
<head>Existing Page</head>
<body>
<div id="login-form"></div>
</body>
</html>
I would like to add the login form configured in my spring-security.xml inside the login-form div.
Typically I believe people just put the form right in the page (i.e. within the div you have there.) Something like:
<form id="blah" action="/j_spring_security_check">
<input type="text" name="j_username" />
<input type="text" name="j_password" />
<input type="submit" name="submit" value="login" />
<input type="reset" name="reset" />
</form>
Then use your css to make it fit the look and feel of the rest of your application.

Resources