Sunday, 22 May 2016

Spring MVC Application



HelloYogiController.java

package com.yug.test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

@Controller

public class HelloYogiController{

@InitBinder
public void initBinder(WebDataBinder  binder){
//binder.setDisallowedFields(new String[]{"password"});
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy****mm**dd");
binder.registerCustomEditor(Date.class, "dob",new CustomDateEditor(dateFormat, false));
binder.registerCustomEditor(String.class, "username", new nameEditor());
}

@RequestMapping(value="/admissionfrom.html", method= RequestMethod.GET)
public ModelAndView getFormPage() {
ModelAndView mav=new ModelAndView("Admissionfrom");

return mav;
}

@ModelAttribute
public void addingCommonObjects(Model m){
m.addAttribute("msg", "Welcome to Spring MVC Applications using @ModelAttribute");
}

@RequestMapping(value="/submitformpage.html", method= RequestMethod.POST)
public ModelAndView getFormResult(@Valid @ModelAttribute("emp") Employee e, BindingResult result) {

if(result.hasErrors()){
ModelAndView mav=new ModelAndView("Admissionfrom");
return mav;
}
ModelAndView mav=new ModelAndView("HelloPage");

return mav;
}

}

Employee.java

package com.yug.test;

import java.util.ArrayList;
import java.util.Date;

import javax.validation.constraints.Max;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

public class Employee {


private String username;

@Size(min=3, max=11, message="Password size must be between {min} and {max}")
@isYogiFieldValidator(listOfValidPasswords="java|.net|sql")
private String password;

@Max(2222)
private long mobile;
@Past
private Date dob;

private ArrayList<String> skills;
private Address empaddr;
public Address getEmpaddr() {
return empaddr;
}
public void setEmpaddr(Address empaddr) {
this.empaddr = empaddr;
}
public long getMobile() {
return mobile;
}
public void setMobile(long mobile) {
this.mobile = mobile;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public ArrayList<String> getSkills() {
return skills;
}
public void setSkills(ArrayList<String> skills) {
this.skills = skills;
}



public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}


}

Address.java

package com.yug.test;

public class Address {
private String country;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPincode() {
return pincode;
}
public void setPincode(int pincode) {
this.pincode = pincode;
}
private String city;
private int pincode;

}

FieldValidator.java

package com.yug.test;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class FieldValidator implements ConstraintValidator<isYogiFieldValidator, String>{
String islistOfValidpwd;
@Override
public void initialize(isYogiFieldValidator isFieldValidator){
this.islistOfValidpwd = isFieldValidator.listOfValidPasswords();
}
public boolean isValid(String skills, ConstraintValidatorContext ctx) {
if(skills == null){
return false;
}
if(skills.matches(islistOfValidpwd)){
return true;
}else{
return false;
}
}

}


isYogiFieldValidator.java

package com.yug.test;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

@Documented
@Constraint(validatedBy= FieldValidator.class)
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)

public @interface isYogiFieldValidator {

//String listOfValidPasswords() default "java|.net|sql"; 
String listOfValidPasswords();  
String message() default "Please, provide skills which you have, Accepted passwords are java, .net, and sql";
Class<?>[] groups() default{};
Class<? extends Payload>[] payload() default{};
}


nameEditor.java

package com.yug.test;

import java.beans.PropertyEditorSupport;

public class nameEditor extends PropertyEditorSupport {

@Override
public void setAsText(String username)throws IllegalArgumentException{
if(username.contains("Mr.") || username.contains("Mrs.")){
setValue(username);
}else{
username="Ms."+username;
setValue(username);
}
}
}


AdmissionForm.java

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1><b>${msg}</b></h1>
 <form:errors path="emp.*"/>  
<form action="/firstSpringMVCEx/submitformpage.html" method="post">
<table>
<td><tr>Enter User Name :</tr>
</td><td><tr><input type="text" name="username"></tr></td><br><br>
<td><tr>Enter User - Pwd :</tr>
</td><td><tr><input type="password" name="password"></tr></td><br><br>
<td align="right"><tr> </tr>

<td><tr>Enter ur mobile # :</tr>
</td><td><tr><input type="text" name="mobile"></tr></td><br><br>

<td><tr>Enter User DOB :</tr>
</td><td><tr><input type="text" name="dob"></tr></td><br><br>
<td><tr>Enter Skills set :</tr>
</td><td><tr><select name="skills" multiple="multiple">
<option value="java">Java</option>
<option value=".net">.Net</option>
<option value="sql">SQL</option>

</tr></td><br><br>

</td><td align="center">
</table>
<table>
<tr>
<td>Country :</td><td><input type="text" name="empaddr.country"/></td></tr>
<tr>
<td>City :</td><td><input type="text" name="empaddr.city"/></td></tr>
<tr>
<td>Pincode :</td><td><input type="text" name="empaddr.pincode"/></td></tr>
</table>
<tr><input type="submit" value="submit"></tr></td>
</form>
</body>
</html>

empMassages.properties

Size = please, Enter a value for {0} field between {2} and {1} and dynamically that is cacheSeconds property.
#Size = please, Enter a value for password field between min and max and only size annotation is using.
#Size.java.lang.String = please, Enter a value for password field between 2 and 10 and type added.
#Size.password = please, Enter a value for password field between 2 and 10 and reference removed.
#Size.emp.password = please, Enter a value for password field between 2 and 10.

HelloPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<h1><b>${msg}</b></h1>


<table>
<tr>
<td>user name is : </td>
<td>${emp.username}</td>
</tr>
<tr>
<td>user pwd is : </td>
<td>${emp.password}</td>
</tr>
<tr>
<td>user mobile is : </td>
<td>${emp.mobile}</td>
</tr>
<tr>
<td>user DOB is : </td>
<td>${emp.dob}</td>
</tr>
<tr>
<td>user skills is : </td>
<td>${emp.skills}</td>
</tr>

<tr>
<td>Employee Address :</td>
<td>
County :${emp.empaddr.country}
City :${emp.empaddr.city}
Pincode :${emp.empaddr.pincode}
</td></tr>
</table>
</body>
</html>

spring-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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 ">

  <context:component-scan base-package="com.yug.test"></context:component-scan> 
 <mvc:annotation-driven></mvc:annotation-driven>
 
 
 <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="prefix">  
      <value>/WEB-INF/</value>  
    </property>  
    <property name="suffix">  
      <value>.jsp</value>  
    </property>  
  </bean> 
  
  <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <property name="basename" value="/WEB-INF/empMessages"></property>
  <property name="cacheSeconds" value="1"></property>
  </bean> 
</beans>  

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>firstSpringMVCEx</display-name>
   
  <servlet>
  <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
  </servlet-mapping>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>


SQL Practise

create table std_table(stno number ,stname varchar2(20),stcity varchar2(20),pincode number ,address varchar2(30), fee varchar2(10),company varchar2(20) )

select * from student_table

desc student_table

--=========DDL================Dr.CAT============

alter table student_table modify pincode number(6)

alter table student_table add fee number;

alter table student_table drop column company

alter table student_table rename column stcity to city

alter table student_table rename to std_table

alter table std_table rename to student_table

--==========================================

insert into student_table values(100,'yug','pune',503183,'addr3',25000)

truncate table student_table

drop table student_table

flashback table test to before drop

create table emp_tab(eid number,ename varchar2(20),)

select * from emp_tab

drop table emp_tab purge

--================DML========SUDI======

insert into student_table values(100,'yug','pune',507183,'addr1')

insert into student_table values(&stno,'&stname','&city',&pincode,'&address')

insert into student_table(stno,pincode) values(&stno,&pincode)

update student_table set fee=22000 where stno='104'

--update student_table set stname='yugandhar' where address=null  can not update

select * from student_table

select stno A,stname B,city C,pincode D,address E from student_table

select stno student_number, stno+100 test_no from student_table where stno>102

select * from student_table order by stno [desc];

select rowid, rownum from student_table

select sum(fee) from student_table

select avg(fee) from student_table

select min(fee) from student_table

select max(fee) from student_table

select 2+3+5/2 from dual;

select trunc(sysdate) from dual;

select count(*) from student_table

select count(stname) from student_table

select count(distinct stname) from student_table

select * from user_tab_columns where table_name='student_table'

desc user_tab_columns

select student_table.* from STUDENT_TABLE;

select student_table.*,nvl(address,'myaddr') from student_table;

select student_table.*,nvl2(address,'myaddr','nullAddr') from student_table;

select student_table.*,nvl2(city,'pune','hyd') from student_table;

select student_table.*,nvl2(stname,'fist','second') from student_table;

select stno , length(stno), city, length(city), nullif(length(stno),length(city)) from student_table;

select stname , length(stname), city, length(city), nullif(length(stname),length(city)) from student_table;

select stno, count(*) from student_table group by stno;

select stno, count(*) from student_table group by stno having count(*)>1;

select stno, count(*),sum(fee) from student_table group by stno;

select stno, count(*),sum(fee),max(fee),min(fee),avg(fee) from student_table group by stno;

create sequence s2 minvalue 100 maxvalue 110 increment by 3;

create table seq_test1(id number , name varchar2(10));

insert into  seq_test values(s2.nextval, 'yog6')

select * from seq_test

alter sequence s2 maxvalue 115;

select * from user_sequences

select * from student_table

create table std as select * from student_table

select * from std

create table std1 as select * from student_table where 1=2

select * from std1

insert into std1 select * from student_table

update std1 set fee=17000 where stno='addr2'