Java

Sunday, 7 October 2012

Spring MVC AbstractCommandController -Validation

   
TUTIORALS FROM MADHAV:


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

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


Spring MVC  

Tepes of controllers:
  1. ·         Controller 
  2.           AbstractCommandController
  3. ·         SimpleFormController
  4. ·         WizardFormController
  5. ·         MultiActionController

Example program 3:
//this program will describes working "AbstractCommandController"validator.:

Login.html
<!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>Softech computer education</title>
<head>
<body>
<form action="login.spring"><pre>
user name:<input type="text" name="uname"/>
password :<input type="password" name="pass"/>
  <input type ="submit" value="login"/>
</pre>
</form>
</body>
</html>
Hugo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
<body>
 wel come to the softech home page
 user name:<%=request.getParameter("uname")  %>
 </body>
 </html>
UserHome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
<body>
 wel come to the  non softech home page
 user name:<%=request.getParameter("uname")  %>
 </body>
 </html>
UserDetails.java
package madhav;
public class UserDetails {
                private String uname,pass;
                public String getUname() {
                                return uname;
                }
                public void setUname(String uname) {
                                this.uname = uname;
                }
                public String getPass() {
                                return pass;
                }
                public void setPass(String pass) {
                                this.pass = pass;
                }
               
}
LoginController.java
package madhav;
import org.springframework.web.servlet.*;
import org.springframework.web.servlet.mvc.*;
import org.springframework.validation.*;
import javax.servlet.http.*;
public class LoginController extends AbstractCommandController
{
               
                public ModelAndView handle(HttpServletRequest req,HttpServletResponse res,Object command,BindException errors)throws Exception
                {
                if(errors.hasErrors())
                                {
                                                PrintWriter out=res.getWriter();
                                                out.println("errors has been found");
                                               
                                                java.util.List<FieldError> errorslist=errors.getFieldErrors("uname");
                                                for(FieldError error:errorslist)
                                                {
                                                                out.println(error.getDefaultMessage());
                                                }
                                                errorslist=errors.getFieldErrors("pass");
                                               
                                                for(FieldError error:errorslist)
                                                {
                                                                out.println(error.getDefaultMessage());
                                                }
                                                return null;
                                               
                                }
                                String type;
                                UserDetails user=((UserDetails)command);
                                type=user.getUname();
                                if(type==null)
                                                return new ModelAndView("/Login.html");
                                                else if(type.equals("hugo"))
                                                                return new ModelAndView("/hugo.jsp");
                                                else
                                                                return new ModelAndView("/UserHome.jsp");
                                                               
                   
                   
                }
}
Dispatcher-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"
                xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
              
               
                <bean id="mad" class="madhav.LoginController">
               
                <property name="commandClass">
               <value type="java.lang.Class">
                madhav.UserDetails
                </value>
                </property>
                <property name="commandName">
                <value>UserDetails</value>
                </property>
                <property name="validatior">
                <bean clss=”madhav.UserValidator”>
                </property>
                </bean>
                <bean id="myurlmapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                <property name="mappings">
                <props>
               <prop key="/login.spring">mad</prop>
                </props>
                </property>
                </bean>
                </beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
                xmlns="http://java.sun.com/xml/ns/j2ee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID">
                <display-name>speingmvcacc</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>index.htm</welcome-file>
</welcome-file-list>
</web-app>
OUT PUT SHORTS:




1 comment:

  1. Programm is not downloading and some java code is also missing like uservalidator.java,Adminhome.jsp...etc

    ReplyDelete