Followers

spring MVC : Spring Forms

web.xml
=====

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

   
    <context:component-scan base-package="myspring" />
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
</beans>

index.jsp
======

<jsp:forward page="login.htm" ></jsp:forward>


User.java
======

package myspring;

public class User {
    private String name;
    private String passWord;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
  
}

MyController.java
===========

package myspring;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/login.htm")
public class MyController {
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap map){
        User user1 = new User();
        map.addAttribute("user", user1);   
    return "login";
    }
   
    @RequestMapping(method = RequestMethod.POST)
    //public String submitForm(@ModelAttribute User user){
    public String submitForm(@ModelAttribute("user") User user){
        if(user.getName().equals("admin") && user.getPassWord().equals("admin")){
    return "success";
        }
   
    return "failure";
    }
}


login.jsp
=====

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form action="login.htm" method="POST" commandName="user" >
   Enter the user name : <form:input path="name" /> <br />
   Enter the pass word : <form:password path="passWord" /> <br />
   <input type="submit" value="Login" />
   
</form:form>


success.jsp
=======

Hi ${user.name} how are you?


failure.jsp
======


Login failed....

No comments:

Post a Comment