Showing posts with label Spring Projects. Show all posts
Showing posts with label Spring Projects. Show all posts

Sunday, February 1, 2015

Spring Simple Basic HelloWorld Application

 Spring Hello World Example Basic Example

Student.java


package com.spring;

public class Student {  
private String name; 
private String Address;
private String Course;
private int Age;
  
public String getCourse() {
 return Course;
}

public void setCourse(String course) {
 Course = course;
}

public int getAge() {
 return Age;
}

public void setAge(int age) {
 Age = age;
}

public String getAddress() {
 return Address;
}

public void setAddress(String address) {
 Address = address;
}

public String getName() {  
    return name;  
}  
  
public void setName(String name) {  
    this.name = name;  
}  
  
public void displayInfo(){  
    System.out.println("Hello: "+name);
    System.out.println("UR Adress :"+Address);
    System.out.println("UR Course :"+Course);
    System.out.println("UR Age :"+Age);
}  
}  







MyBeans.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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">  
  
<bean id="sb" class="com.spring.Student">  
<property name="name" value="Naresh Nalla"></property>
<property name="Address" value="Metpally"></property>
<property name="Course" value="Mtech CSE"></property> 
<property name="Age" value="24"></property> 
</bean>  

</beans>  






TestCase.java

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.spring.Student;
  
public class TestCase {  
public static void main(String[] args) {  
 Resource resource=new ClassPathResource("MyBeans.xml");  
    BeanFactory beans=new XmlBeanFactory(resource);  
      
    Student student=(Student)beans.getBean("sb");  
    student.displayInfo();  
}  
}  

Folder Structure to run:

Wednesday, October 15, 2014

Login spring mvc with out jdbc database

Login spring mvc with out jdbc database

UserDAO.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package com.nrs.spring.dao.dummy;

import com.nrs.spring.dao.UserDAOI;

public class UserDAO implements UserDAOI{

 @Override
 public boolean findUser(String uname, String pass) {
  return (uname.equals("naresh")&&pass.equals("nareshpass"));
 }
 
}

UserHome.jsp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<%@ 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> Hello </h1> ${param.uname }

</body>
</html>

LoginForm.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!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>
<form action="login.spring"  >
<pre>
UserName : <input type="text" name="uname">
Password : <input type="password" name="pass" >
<input type="submit" value=Login >
</pre>
</form>

</body>
</html>


LoginController.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.nrs.spring.web;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.nrs.spring.dao.UserDAOI;

public class LoginController implements Controller {

  

 @Override
 public ModelAndView handleRequest(HttpServletRequest req,
   HttpServletResponse res) throws Exception {
  
  String uname=req.getParameter("uname");
  String pass=req.getParameter("pass");
  
  boolean flag=udao.findUser(uname,pass);
  System.out.println(flag);
  if(flag){
   return new ModelAndView("/UserHome.jsp");
  }else{
   return new ModelAndView("/LoginForm.html");
   
  }
 }
private UserDAOI udao;
 
 public void setUserDAO(UserDAOI udao) {
   this.udao = udao;
  }
 
 
}


applicationContext-dao-dummy.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<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:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"  
    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">  

<bean id="udao" class="com.nrs.spring.dao.dummy.UserDAO" />

</beans>


ds-servlet.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<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:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"  
    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">  
<bean id="/login.spring" class="com.nrs.spring.web.LoginController" p:UserDAO-ref="udao" />

</beans>

web.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<web-app>
<servlet>
<servlet-name>ds</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
 /WEB-INF/ds-servlet.xml
 /WEB-INF/applicationContext-dao-dummy.xml
 </param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ds</servlet-name>

<url-pattern>*.spring</url-pattern>
</servlet-mapping>

</web-app>

Output screens:

1
2
3
4
5
6
7
package com.nrs.spring.dao;

public interface UserDAOI {
 
 boolean findUser(String uname, String pass);

}