Wednesday, July 31, 2024

JAVA-II Assignment-5

 Assignment 5: Spring


Set A

a) Create a Spring core example to display the message “If you can't explain it simply, you don't understand it well enough”.

# hellobeans.java

package springcore_example;

public class hellobean {

    private String message;

    public String getMessage() {

        return message;

    }

    public void setMessage(String message) {

        this.message = message;

    }

    public void sayHello() {

        System.out.println(getMessage());

    }

}


# Main.java

package springcore_example;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {

        @SuppressWarnings("resource")

        ApplicationContext context = new 

ClassPathXmlApplicationContext("beans.xml");

        hellobean hello = (hellobean) context.getBean("helloWorld");

        hello.sayHello();

    }



}

# beans.xml

}

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       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-3.0.xsd">

    <bean id="helloWorld" class="springcore_example.hellobean">

        <property name="message" value="If you can't explain it simply, 

you don't understand it well enough"/>

    </bean>

</beans>

}

# beans.xml

}

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       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-3.0.xsd">

    <bean id="helloWorld" class="springcore_example.hellobean">

        <property name="message" value="If you can't explain it simply, 

you don't understand it well enough"/>

    </bean>

</beans>


b) Write a program to display the Current Date using spring.

import java.time.*;

import java.time.format.DateTimeFormatter;

public class CurrentTimeJava8 {

   public static void getCurrentTime(){

        System.out.println("-----Current time of your time zone-----");

        LocalTime time = LocalTime.now();

        System.out.println("Current time of the day: " + time);

    }

    public static void getCurrentTimeWithTimeZone(){

        System.out.println("-----Current time of a different time zone using LocalTime-----");

        ZoneId zoneId = ZoneId.of("America/Los_Angeles");

        LocalTime localTime=LocalTime.now(zoneId);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

        String formattedTime=localTime.format(formatter);

        System.out.println("Current time of the day in Los Angeles: " + formattedTime);

    }

    public static void getCurrentTimeWithOffset(){

        System.out.println("-----Current time of different offset-----");

        ZoneOffset zoneOffset = ZoneOffset.of("-08:00");

        ZoneId zoneId=ZoneId.ofOffset("UTC", zoneOffset);

        LocalTime offsetTime = LocalTime.now(zoneId);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");

        String formattedTime=offsetTime.format(formatter);

        System.out.println("Current time of the day with offset -08:00: " + formattedTime);

    }

}


Set B

a) Design simple student information like Student_id, Student_Name and Student_Age using Spring Framework.

# hellobean.java

package springcore_example;

import java.util.List;

public class hellobean {

    private List<Student> students;

    public List<Student> getStudents() {

        return students;

    }

    public void setStudents(List<Student> students) {

        this.students = students;

    }

    public void printStudents() {

        for (Student student : students) {

            System.out.println("Student ID is: "+student.getId()+student.getName() + " 

is " + student.getAge() + " years old.");

        }

# hellobean.java

package springcore_example;

import java.util.List;

public class hellobean {

    private List<Student> students;

    public List<Student> getStudents() {

        return students;

    }

    public void setStudents(List<Student> students) {

        this.students = students;

    }

    public void printStudents() {

        for (Student student : students) {

            System.out.println("Student ID is: "+student.getId()+student.getName() + " 

is " + student.getAge() + " years old.");

        }

}

}


# Main.java

package springcore_example;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {

        ApplicationContext context = new 

ClassPathXmlApplicationContext("beans.xml");

        hellobean hello = (hellobean) context.getBean("helloWorld");

        hello.printStudents();

    }

}

# Student.java

package springcore_example;

public class Student {

    private int id;

    private String name;

    private int age;

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

this.name = name;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       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-3.0.xsd">

    <bean id="student1" class="springcore_example.Student">

    <property name="id" value="1"/>

    <property name="name" value="Shahrukh"/>

    <property name="age" value="20"/>

</bean>

<bean id="student2" class="springcore_example.Student">

    <property name="id" value="2"/>

    <property name="name" value="Shubham"/>

    <property name="age" value="22"/>

</bean>

<bean id="student3" class="springcore_example.Student">

    <property name="id" value="3"/>

    <property name="name" value="Rahul"/>

    <property name="age" value="23"/>

</bean>

<bean id="helloWorld" class="springcore_example.hellobean">

    <property name="students">

        <list>

            <ref bean="student1"/>

            <ref bean="student2"/>

                        <ref bean="student3"/>

        </list>

    </property>

</bean>

</beans>



b) Design the Employee login form application using spring form MVC validation.


index.jsp

<a href="empform">Add Employee</a>  

<a href="viewemp">View Employees</a>  

Emp.java

package com.javatpoint.beans;  

public class Emp {  

private int id;  

private String name;  

private float salary;  

private String designation;  

  

public Emp() {}  

  

public Emp(int id, String name, float salary, String designation) {  

    super();  

    this.id = id;  

    this.name = name;  

    this.salary = salary;  

    this.designation = designation;  

}  

  

public int getId() {  

    return id;  

}  

public void setId(int id) {  

    this.id = id;  

}  

public String getName() {  

    return name;  

}  

public void setName(String name) {  

    this.name = name;  

}  

public float getSalary() {  

    return salary;  

}  

public void setSalary(float salary) {  

    this.salary = salary;  

}  

public String getDesignation() {  

    return designation;  

}  

public void setDesignation(String designation) {  

    this.designation = designation;  

}  

  

}  

 EmpController.java

package com.javatpoint.controllers;  

  

import java.util.ArrayList;  

import java.util.List;  

import org.springframework.stereotype.Controller;  

import org.springframework.web.bind.annotation.ModelAttribute;  

import org.springframework.web.bind.annotation.RequestMapping;  

import org.springframework.web.bind.annotation.RequestMethod;  

import org.springframework.web.servlet.ModelAndView;  

import com.javatpoint.beans.Emp;  

@Controller  

public class EmpController {  

  

    @RequestMapping("/empform")  

    public ModelAndView showform(){  

         //command is a reserved request attribute name, now use <form> tag to show object data  

        return new ModelAndView("empform","command",new Emp());  

    }  

    @RequestMapping(value="/save",method = RequestMethod.POST)  

    public ModelAndView save(@ModelAttribute("emp") Emp emp){  

        //write code to save emp object  

        //here, we are displaying emp object to prove emp has data  

        System.out.println(emp.getName()+" "+emp.getSalary()+" "+emp.getDesignation());  

          

        //return new ModelAndView("empform","command",emp);//will display object data  

        return new ModelAndView("redirect:/viewemp");//will redirect to viewemp request mapping  

    }  

      

    @RequestMapping("/viewemp")  

    public ModelAndView viewemp(){  

        //write the code to get all employees from DAO  

        //here, we are writing manual code of list for easy understanding  

        List<Emp> list=new ArrayList<Emp>();  

        list.add(new Emp(1,"rahul",35000f,"S.Engineer"));  

        list.add(new Emp(2,"aditya",25000f,"IT Manager"));  

        list.add(new Emp(3,"sachin",55000f,"Care Taker"));  

          

        return new ModelAndView("viewemp","list",list);  

    }  

}  

web.xml

<?xml version="1.0" encoding="UTF-8"?>  

<web-app version="2.5"   

    xmlns="http://java.sun.com/xml/ns/javaee"   

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  

 <servlet>  

    <servlet-name>spring</servlet-name>  

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  

    <load-on-startup>1</load-on-startup>  

</servlet>  

<servlet-mapping>  

    <servlet-name>spring</servlet-name>  

    <url-pattern>/</url-pattern>  

</servlet-mapping>  

</web-app>  

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  

<beans xmlns="http://www.springframework.org/schema/beans"    

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    

    xmlns:p="http://www.springframework.org/schema/p"    

    xmlns:context="http://www.springframework.org/schema/context"    

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

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    

http://www.springframework.org/schema/context    

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

  

<context:component-scan base-package="com.javatpoint.controllers"></context:component-scan>  

  

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  

<property name="prefix" value="/WEB-INF/jsp/"></property>  

<property name="suffix" value=".jsp"></property>  

</bean>  

  

</beans>  

empform.jsp

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>    

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>    

  

       <form:form method="post" action="save">    

        <table >    

         <tr>    

          <td>Name : </td>   

          <td><form:input path="name"  /></td>  

         </tr>    

         <tr>    

          <td>Salary :</td>    

          <td><form:input path="salary" /></td>  

         </tr>   

         <tr>    

          <td>Designation :</td>    

          <td><form:input path="designation" /></td>  

         </tr>   

         <tr>    

          <td colspan="2"><input type="submit" value="Save" /></td>    

         </tr>    

        </table>    

       </form:form>    

viewemp.jsp

   <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>    

   <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>    

  

  

<table border="2" width="70%" cellpadding="2">  

<tr><th>Id</th><th>Name</th><th>Salary</th><th>Designation</th></tr>  

   <c:forEach var="emp" items="${list}">   

   <tr>  

   <td>${emp.id}</td>  

   <td>${emp.name}</td>  

   <td>${emp.salary}</td>  

   <td>${emp.designation}</td>  

   </tr>  

   </c:forEach>  

   </table>  

No comments:

Post a Comment