Академический Документы
Профессиональный Документы
Культура Документы
step 4. Don't Enter username and password and submit the "submit" button.
step 5. Now Enter username and don't enter password and submit the "submit"
button.
step 6. Now Enter username and password both and submit the "submit" button.
You will redirect to success page
Step b. The JSP page fills in the form fields with the information in the
"command" object.
The JSP page fills in the form fields with the information in the "command"
object.
In our case "command" object is userBean
<property name="commandName"><value>userBean</value></property>
<property name="commandClass"><value>com.UserBean</value></property>
When the form is submitted, the details are passed to the Validator servlet bean
for validation.
In our case "logonFormValidator"
<property name="validator"><ref bean="logonFormValidator"/></property>
Step c. This checks the form data for errors. If there are no errors, it
returns control directly to the Controller servlet bean. onSubmit() method
of LogonController
Step d. If there were errors in the form, then the Validator servlet bean
calls the JSP page again. This time, the "command" object contains the
data previously entered by the user, so the JSP page fills in the form fields
exactly as before.
<property name="sessionForm"><value>true</value></property>
Keep command object throughout session
validator:
<bean id="logonFormValidator" class="com.LogonFormValidator"/>
Step 3. Controller classe
"/test/logonPage.do" call the formBackingObject(HttpServletRequest request) of
the controller.
formBackingObject(HttpServletRequest request) method set the FormBean with
the default value. This method is used to display the form.
And onSubmit() method will be called after click on submit button of the form.
LogonController.java
package com;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.SimpleFormController;
return backingObject;
}
import
org.springframework.validation.Errors;
import
org.springframework.validation.Validator;
}
}
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String
password) {
this.password = password;
}
/**
* @return Returns the userName.
*/
public String getUserName() {
return userName;
}
/**
* @param userName The userName to set.
*/
public void setUserName(String
userName) {
this.userName = userName;
}
}
<html>
<head><title>techfaq360 Spring Validation
Example</title></head>
<body>
<center>
<form method="post"
action="/springvalidation/test/logonPage.do">
<table width="25%" border="1">
<tr>
<td align="center" bgcolor="lightblue">Log on</td>
</tr>
<tr>
<td>
<table border="0" width="100%">
<tr>
<td width="33%" align="right">Username: </td>
<td width="66%" align="left">
<spring:bind path="userBean.userName">
<input type="text"
name="userName"
value="<core:out value=""/>"/>
</spring:bind>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<spring:hasBindErrors name="userBean">
<font color="red"><core:out value=""/></font>
</spring:hasBindErrors>
</td>
</tr>
<tr>
<td width="33%" align="right">Password: </td>
<td width="66%" align="left">
<spring:bind path="userBean.password">
<input type="password" name="password" />
</spring:bind>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<spring:hasBindErrors name="userBean">
<font color="red"><core:out value=""/></font>
</spring:hasBindErrors>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" alignment="center" value="Submit">
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
succes.jsp
<h2>Logon Success </h2>
MessageSources
Spring currently provides two MessageSource implementations. These are the
ResourceBundleMessageSource and the StaticMessageSource.
How to implement message Resource : Follow the steps below
Step 1. Add the below XML to your springexample-test.xml ( config xml
file).
<bean id="messageSource"
class="org.springframework.context.support.Res
ourceBundleMessageSource">
<property name="basenames">
<list>
<value>applicationprop</value>
</list>
</property>
<bean>
Step 2. Example.java .
In the Example.java we will display the messages from properties file .
package com.techfaq;
public class Example {
out put is :
Password Required!
UserName is required
ApplicationContext
The basis for the context package is the ApplicationContext interface, located in
the org.springframework.context package. Deriving from the BeanFactory
interface, it provides all the functionality of BeanFactory. To allow working in a
more framework-oriented fashion, using layering and hierarchical contexts, the
context package also provides the following functionality:
</beans>
/// DO METHODS
}
the system service like logging, transaction management, security etc., must be included in
the program in general case.
AOP makes it possible to modularize and separate these services and then apply them
declaratively to the components and we can focus on our own specific concerns.
Transaction parameters are like EJB
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED_NEW etc
What is RowCallbackHandler ?
What is PreparedStatementCreator ?
PreparedStatementCreator:
Is one of the most common used interfaces for writing data to database.
Has one method ? createPreparedStatement(Connection)
Responsible for creating a PreparedStatement.
Does not need to handle SQLExceptions.
1) InitializingBean
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean {
public void init() { // do some initialization work }
}
OR
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements InitializingBean {
public void afterPropertiesSet() { // do some initialization work }
}
2) DisposableBean
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-
method="cleanup"/>
public class ExampleBean {
public void cleanup() {
// do some destruction work (like releasing pooled connections)
}}
OR
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements DisposableBean {
public void destroy() { // do some destruction work (like releasing pooled connections) }
}
*Loose coupling: Components can added Declaratively so we can add and remove the
components with out code change.
For Example :
<bean id="createCreditCard"
class="springexample.creditcardaccount.CreateCreditCardAccount">
<property name="emailInterface">
<ref bean="email" />
</property>
<property name="smsInterface">
<ref bean="sms" />
</property>
<property name="daoInterface">
<ref bean="dao" />
</property>
</bean>
*Not Required any singletons : Don't need to code for singleton class. Every class is by
default singleton. you can make not singleton
by making singleton="false"
<bean id="sms" class="springexample.sms.SMS" singleton="false">
</bean>