0% found this document useful (0 votes)
3 views10 pages

Java Assignment 5

The document provides examples of Spring framework applications, including displaying a message, showing the current date, and managing student information. It also includes a simple employee login form using Spring MVC with validation. Each example consists of Java classes, XML configuration files, and JSP pages to demonstrate the functionality.

Uploaded by

Dhruv Khobre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Java Assignment 5

The document provides examples of Spring framework applications, including displaying a message, showing the current date, and managing student information. It also includes a simple employee login form using Spring MVC with validation. Each example consists of Java classes, XML configuration files, and JSP pages to demonstrate the functionality.

Uploaded by

Dhruv Khobre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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”.
Code:
[Link]
public class MessageBean {
private String message;
public void setMessage(String message) {
[Link] = message;
}
public void display() {
[Link](message);
}
}

[Link]
import [Link];
import [Link];
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
MessageBean obj = (MessageBean) [Link]("msg");
[Link]();
}
}
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]

<bean id="msg" class="MessageBean">


<property name="message"
value="If you can't explain it simply, you don't understand it well enough"/>
</bean>
</beans>

Output:
b) Write a program to display the Current Date using spring.
Code:
[Link]
import [Link];
public class DateBean {
public void showDate() {
Date d = new Date();
[Link]("Current Date: " + d);
}
}

[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
<bean id="dateBean" class="DateBean"/>
</beans>
[Link]
import [Link];
import [Link];

public class MainApp {


public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
DateBean obj = (DateBean) [Link]("dateBean");
[Link]();
}
}

Output:
SET B

a) Design simple student information like Student_id, Student_Name and


Student_Age using Spring Framework.
Code:
[Link]
public class Student {
private int studentId;
private String studentName;
private int studentAge;
public void setStudentId(int studentId) {
[Link] = studentId;
}
public void setStudentName(String studentName) {
[Link] = studentName;
}
public void setStudentAge(int studentAge) {
[Link] = studentAge;
}
public void display() {
[Link]("Student ID: " + studentId);
[Link]("Student Name: " + studentName);
[Link]("Student Age: " + studentAge);
}
}
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
<bean id="student" class="Student">
<property name="studentId" value="101"/>
<property name="studentName" value="Rohit Patil"/>
<property name="studentAge" value="21"/>
</bean>
</beans>
[Link]
import [Link];
import [Link];
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Student s = (Student) [Link]("student");
[Link]();
}
}
Output:
b) Design the Employee login form application using spring form MVC
validation.
Code:
[Link]
<%@ taglib uri="[Link] prefix="form" %>
<h2>Employee Login</h2>
<form:form method="post" modelAttribute="employee">
Username: <form:input path="username"/><br><br>
Password: <form:password path="password"/><br><br>
<input type="submit" value="Login">
</form:form>
[Link]
<h3>Login Successful</h3>
Welcome Employee
<h3>Login Successful</h3>
Welcome Employee

[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:mvc="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
<mvc:annotation-driven/>
<!-- MANUAL CONTROLLER REGISTRATION -->
<bean class="EmployeeController"/>
<bean
class="[Link]">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

[Link]
<web-app>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
[Link]
</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>
[Link]
public class Employee {
private String username;
private String password;

public String getUsername() {


return username;
}
public void setUsername(String username) {
[Link] = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
[Link] = password;
}
}

[Link]
import [Link];
import [Link];
import [Link].*;

@Controller
public class EmployeeController {
@RequestMapping("/login")
public String login(Model model) {
[Link]("employee", new Employee());
return "login";
}
}

Output:

You might also like