Eclipse环境下 spring MVC 入门实例

实现一个spring 的登陆事例

1.在eclipse下新建动态网页工程springmvc
 2.需要导入jar包.
    aopalliance.jar;commons-beanutils.jar;commons-collections.jar;commons-dbcp.jar;commons-digester.jar;
commons-lang.jar;commons-logging.jar;commons-pool.jar;commons-validator.jar;freemarker.jar;jaxen-1.1-beta-7.jar;
jstl.jar;junit.jar;log4j-1.2.13.jar;servlet-api.jar;spring.jar;spring-aspects.jar;spring-mock.jar;spring-web.jar
standard.jar;xalan.jar;xercesImpl.jar;xml-apis.jar
 3.修改web.xml
<? xml version="1.0" encoding="UTF-8" ?>
< web-app  id ="WebApp_ID"  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" >
    
< display-name >
    springmvc
</ display-name >
    
< servlet >
           
< servlet-name > Dispatcher </ servlet-name >
           
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
           
           
< init-param >
             
< param-name > contextConfigLocation </ param-name >
             
< param-value > /WEB-INF/Config.xml </ param-value >
           
</ init-param >
    
</ servlet >
    
    
< servlet-mapping >
       
< servlet-name > Dispatcher </ servlet-name >
       
< url-pattern > *.do </ url-pattern >
    
</ servlet-mapping >
    
    
< jsp-config >
       
< taglib >
            
< taglib-uri > http://java.sun.com/jstl/core_rt </ taglib-uri >
            
< taglib-location > /WEB-INF/tlds/c.tld </ taglib-location >
       
</ taglib >
    
</ jsp-config >
</ web-app >
其中jsp-config 定义引用 c.tld标签,所以需要在 WEB-INF目录下新建tlds目录后拷贝c.tld到tlds目录下.

4.对应
< param-value > /WEB-INF/Config.xml </ param-value >在WEB-INF下新建Config.xml
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

< beans >
   
< bean  id ="viewResolver"
        class
="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        
< property  name ="viewClass" >
           
< value >
              org.springframework.web.servlet.view.JstlView
           
</ value >
        
</ property >
        
        
< property  name ="prefix" >
            
< value > /WEB-INF/view/ </ value >
        
</ property >
        
        
< property  name ="suffix" >
           
< value > .ftl </ value >
        
</ property >
        
   
</ bean >
   
 
   
< bean  id ="urlMapping"
        class
="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
        
< property  name ="mappings" >
           
< props >
              
< prop  key ="/login.do" > LoginAction </ prop >
           
</ props >
        
</ property >
   
</ bean >
   
   
< bean  id ="LoginAction"
       class
="net.oscar.action.LoginAction" >
       
< property  name ="commandClass" >
          
< value > net.oscar.action.LoginInfo </ value >
       
</ property >
       
       
< property  name ="fail_view" >
           
< value > loginfail </ value >
       
</ property >
       
       
< property  name ="success_view" >
          
< value > main </ value >
       
</ property >
   
</ bean >
</ beans >

5.在工程的src下新建Config.xml用到的 net.oscar.action.LoginAction和 net.oscar.action.LoginInfo的类
package  net.oscar.action;

public   class  LoginInfo  ... {
    
private String username;
    
private String password;
    
public String getUsername() ...{
        
return username;
    }

    
public void setUsername(String username) ...{
        
this.username = username;
    }

    
public String getPassword() ...{
        
return password;
    }

    
public void setPassword(String password) ...{
        
this.password = password;
    }

}


package  net.oscar.action;

import  java.util.HashMap;
import  java.util.LinkedList;
import  java.util.List;

import  org.springframework.validation.BindException;
import  org.springframework.web.servlet.ModelAndView;
import  org.springframework.web.servlet.mvc.SimpleFormController;

public   class  LoginAction  extends  SimpleFormController ... {
     
private String fail_view;
     
private String success_view;
     
    
protected ModelAndView onSubmit(Object cmd,BindException ex)throws Exception...{
        LoginInfo loginInfo
=(LoginInfo)cmd;
        
if(login(loginInfo)==0)...{
            HashMap result_map
=new HashMap();
            result_map.put(
"logininfo", loginInfo);
            
            List msgList
=new LinkedList();
            msgList.add(
"msg1");
            msgList.add(
"msg2");
            msgList.add(
"msg3");
            result_map.put(
"messages", msgList);
            
            
return new ModelAndView(this.getSuccess_view(),result_map);
        }
else...{
            
return new ModelAndView(this.getFail_view());
        }

        
    }

    
    
private int login(LoginInfo loginInfo)...{
        
if("oscar".equalsIgnoreCase(loginInfo.getUsername())&&"123456".equals(loginInfo.getPassword()))...{
            
return 0;
        }

        
return 1;
        
    }

    
public String getFail_view() ...{
        
return fail_view;
    }

    
public void setFail_view(String fail_view) ...{
        
this.fail_view = fail_view;
    }

    
public String getSuccess_view() ...{
        
return success_view;
    }

    
public void setSuccess_view(String success_view) ...{
        
this.success_view = success_view;
    }

}


6.改动index.html文件(在WebContent目录下)
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >

< title > Insert title here </ title >
</ head >
< body >
   
< form  method ="post"  action ="login.do" >
        
< align ="center" > 登录 </ p >
        
< br >
        用户名:
        
< input  type ="text"  name ="username" >
        
< br >
        密
&nbsp;&nbsp; 码:
        
< input  type ="password"  name ="password" >
        
< br >
        
< p >
           
< input  type ="submit"  value ="提交"  name ="B1" >
            
< input  type ="reset"  value ="重置"  name ="B2" >
        
</ p >
   
</ form >
</ body >
</ html >
7.在WEB-INF下新建view目录用于存放jsp文件,loginfail.jsp和main.jsp
main.jsp
<% ... @ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding
="ISO-8859-1"
%>
<% ... @taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>    

< html >
< head >


</ head >
< body >
   
< p > Login Success!!! </ p >
   
   
< p > Current User:
       
< c:out  value ="${logininfo.username}" />< br >
   
</ p >
   
   
< p > Your Current messages: </ p >
   
< c:forEach  items ="${messages}"
              var
="item"
              begin
="0"
              end
="9"
              step
="1"
              varStatus
="var" >
         
< c:if  test ="${var.index%2==0}"   >
         *
         
</ c:if >    
         ${item}
< br >  
   
</ c:forEach >
</ body >
</ html >
loginfail.jsp
<% ... @ 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 >
Login fail!
</ body >
</ html >
 
大功告成!在index.html中输入用户名:oscar 密码:123456就可以登陆了. Eclipse环境下 spring MVC 入门实例_第1张图片

你可能感兴趣的:(eclipse,spring,html,bean,mvc,import)