Spring Framework

         History About php (Preprocessor HyperText)   
         click here  http://en.wikipedia.org/wiki/PHP


php the right way is the best site for learn php and it will teach you about good programming technique visit here learn yourself best of luck
          www.phptherightway.com

Spring Framework

       
Question: 
How to use properties file to load country options
Answer:
This solution will show you how to place the country options in a properties file. The values will no longer be hard coded in the Java code.
1. Create a properties file to hold the countries. It will be a name value pair.  Country code is name. Country name is the value.
New text file:  WEB-INF/countries.properties
BR=Brazil 
FR=France 
CO=Colombia 
IN=India
Note the location of the properties file is very important. It must be stored in WEB-INF/countries.properties
2. Update header section for Spring config file
We are going to use a new set of Spring tags for <util>. As a result, you need to update the header information in the Spring config file.
File: spring-mvc-dmo-servlet.xml
Remove the previous header and add this.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:util="http://www.springframework.org/schema/util" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans.xsd     
            http://www.springframework.org/schema/context     
            http://www.springframework.org/schema/context/spring-context.xsd     
            http://www.springframework.org/schema/mvc         
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/util     
            http://www.springframework.org/schema/util/spring-util.xsd">
3. Load the country options properties file in the Spring config file. Bean id: countryOptions
File: spring-mvc-dmo-servlet.xml
Add the following lines:
<util:properties id="countryOptions" location="classpath:../countries.properties" />
4. Inject the properties values into your Spring Controller: StudentController.java
@Value("#{countryOptions}") 
private Map<String, String> countryOptions;
5. Add the country options to the Spring MVC model. Attribute name: theCountryOptions
@RequestMapping("/showForm") 
public String showForm(Model theModel) { 

    // create a student object Student 
    Student theStudent = new Student();
 
    // add student object to the model 
    theModel.addAttribute("student", theStudent); 

    // add the country options to the model 
    theModel.addAttribute("theCountryOptions", countryOptions); 

    return "student-form"; 
}
6. Update the JSP page, student-form.jsp, to use the new model attribute for the drop-down list: theCountryOptions
<form:select path="country"> 
 <form:options items="${theCountryOptions}" />
</form:select>
7. Remove all references to country option from your Student.java.  


FAQ: How to populate radiobuttons with items from Java class like we did with selectlist?

You can follow a similar approach that we used for the drop-down list.
Here are the steps
1. Set up the data in your Student class
Add a new field
    private LinkedHashMap<String, String> favoriteLanguageOptions;
In your constructor, populate the data
        // populate favorite language options
        favoriteLanguageOptions = new LinkedHashMap<>();
        // parameter order: value, display label
        //
        favoriteLanguageOptions.put("Java", "Java");
        favoriteLanguageOptions.put("C#", "C#");
        favoriteLanguageOptions.put("PHP", "PHP");
        favoriteLanguageOptions.put("Ruby", "Ruby");      
Add getter method
    public LinkedHashMap<String, String> getFavoriteLanguageOptions() {
        return favoriteLanguageOptions;
    }
2. Reference the data in your form
        Favorite Language:
      
        <form:radiobuttons path="favoriteLanguage" items="${student.favoriteLanguageOptions}"  />
Source code is available here:
https://gist.github.com/darbyluv2code/debb69b1bf8010d84d50e0542e809ffb

Core Dependencies <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.13</version>
</dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
  </dependencies>


Configure web.xml 

<servlet>
  <servlet-name>form-tags</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>form-tags</servlet-name>
  <url-pattern>/spring/*</url-pattern>

  </servlet-mapping>

configure servlet.xml 

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--  MAKING ANOOTATION AWARE -->
<context:annotation-config></context:annotation-config>

<!-- SCANNING ONLY SPRING CLASSES -->
<context:component-scan base-package="com.form.tag.demo"></context:component-scan>

<!--  PROJECT IS MVC AWARE  -->
<mvc:annotation-driven></mvc:annotation-driven>


  

<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>


</beans>









Comments

Post a Comment

Popular posts from this blog

Validation Spring

Spring MVC Custom Validation