Java

Friday 12 October 2012

spring mvc annotations working with forms



Ex3:




TUTIORALS FROM MADHAV:
 

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 





index.html


<!DOCTYPE html>
<html>
  <head>
    <title>index.html</title>
     
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
 
  <body bgcolor="wheat">
    This is my HTML page. <br>
    <a href="student.spring">click hear for SimpleFsormController</a>
  </body>
</html>

FormDeatils.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!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>Registration Page</title>
</head>
<body bgcolor="wheat">

<form action="student.spring" method="POST" commandName="student">
      <table>
            <tr>
                  <td>Enter Ur Name :</td>
                  <td><input type="text" name="name"/></td>
            </tr>
     
           
            <tr>
                  <td>Enter Address</td>
                  <td><input type="text" name="address"/></td>
            </tr>
           
            <tr>
                  <td>Enter Mail ID</td>
                  <td><input type="text" name="mailId"/></td>
            </tr>
           
            <tr>
                  <td colspan="2"><input type="submit" value="submit"></td>
            </tr>
      </table>
</form>

</body>
</html>

Success.jsp



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'Success.jsp' starting page</title>
   
      <meta http-equiv="pragma" content="no-cache">
      <meta http-equiv="cache-control" content="no-cache">
      <meta http-equiv="expires" content="0">   
      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
      <meta http-equiv="description" content="This is my page">
      <!--
      <link rel="stylesheet" type="text/css" href="styles.css">
      -->

  </head>
 
  <body>
    form submitted successfully <br>
  </body>
</html>

Student.java

package madhav;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Size;
public class Student {
    @NotEmpty(message = "name must not be blank.")
    @Size(min = 1, max = 10, message = "Password must between 1 to 10")
                                private String name;
    @NotEmpty(message = "address must not be blank.")
    @Size(min = 1, max = 10, message = "Password must between 1 to 10")
                                private String address;
    @NotEmpty(message = "email must not be blank.")
    @Size(min = 1, max = 10, message = "Password must between 1 to 10")
                                private String mailId;
                               
                               
                                public String getName() {
                                    return name;
                                }
                                public void setName(String name) {
                                    this.name = name;
                                }
                                public void setAddress(String address) {
                                    this.address = address;
                                }
                                public String getAddress() {
                                    return address;
                                }
                                public void setMailId(String mailId) {
                                    this.mailId = mailId;
                                }
                                public String getMailId() {
                                    return mailId;
                                }  
}

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:mvc="http://www.springframework.org/schema/mvc"
        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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
      <context:component-scan base-package="madhav" />

      <bean id="viewResolver"

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

<property name="prefix">

<value>/WEB-INF/jspfiles/</value>

</property>

<property name="suffix">

<value>.jsp</value>

</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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringExample6</display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.spring</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>
          

Output  screens:






No comments:

Post a Comment