SpingMVC2

SpringMVC基础知识:



核心类和接口

DispatcherServlet   -- 前置控制器

HandlerMapping接口 -- 处理请求的映射

HandlerMapping接口的实现类:

SimpleUrlHandlerMapping  通过配置文件,把一个URL映射到Controller

DefaultAnnotationHandlerMapping  通过注解,把一个URL映射到Controller类上

HandlerAdapter接口 -- 处理请求的映射

AnnotationMethodHandlerAdapter类,通过注解,把一个URL映射到Controller类的方法上

Controller接口 -- 控制器

由于我们使用了@Controller注解,添加了@Controller注解注解的类就可以担任控制器(Action)的职责,

所以我们并没有用到这个接口。

ViewResolver接口的实现类

UrlBasedViewResolver类 通过配置文件,把一个视图名交给到一个View来处理

InternalResourceViewResolver类,比上面的类,加入了JSTL的支持

ModelAndView类



登录实例:



首先是配置xml:

<servlet>

    <servlet-name>dipatcher</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/applicationContext.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>//是启动顺序和servlettp容器一起启动

  </servlet>

 

  <servlet-mapping>

    <servlet-name>dipatcher</servlet-name>

    <url-pattern>*.do</url-pattern>//拦截以.do结尾的请求

  </servlet-mapping>

其中<param-value>**.xml</param-value> 这里可以使用多种写法
1、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml
2、<param-value>/WEB-INF/classes/springMVC.xml</param-value>
3、<param-value>classpath*:springMVC-mvc.xml</param-value>
4、多个值用逗号分隔

然后配置跟上下文applocationContext.xml

<!-- efinition of View Resolver 视图解析器-->

<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>/view/</value>在当前目录下的view目录下存放视图

    </property>

    <property name="suffix">

       <value>.jsp</value>视图均已.jsp结尾

    </property>

</bean>



<!-- Request Mapping 映射响应器-->

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

    <property name="mappings">

       <props>

           <prop key="/regAction.do">regAction</prop>//注册跳转Action

           <prop key="/login.do">login</prop>//登录跳转Action

       </props>

    </property>

</bean>



<!-- Action Definition  控制器 -->

<bean id="regAction" class="org.lee.springmvc.demo.RegAction">

    <property name="commandClass">

       <value>org.lee.springmvc.demo.RegInfo</value>

    </property>

   

    <property name="error_view">

       <value>error</value>

    </property>

   

    <property name="success_view">

       <value>success</value>

    </property>

</bean>



<!-- Action Definition  控制器 -->

    login为上面视图解析器中的名字

<bean id="login" class="cn.csdn.login.LoginAction">

    <property name="commandClass">

       <value>cn.csdn.login.Login</value>

    </property>

   

    <property name="error">

       <value>error</value>

    </property>

   

    <property name="success">

       <value>loginsuccess</value>//如果成功了跳转到view目录下的loginsuccess.jsp中

    </property>

</bean>

页面为:

<body>

    <div align="center">

         <form action="login.do" method="post">

           <table>

           <tr>

               <td>用户名:</td>

               <td> <input type="text" name="username"> </td>

           </tr>

           <tr>

               <td>密码:</td>

               <td> <input type="password" name="password"> </td>

           </tr>

           <tr>

               <td colspan="2">

                   <input type="submit" value="登录">

               </td>

           </tr>

           </table>

         </form>

      </div>

  </body>



Bean为:

         package cn.csdn.login;



public class Login {



    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;

    }

   

   

}

Action为:

package cn.csdn.login;



import java.util.HashMap;



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 error ;

         private String success;

        

         protected ModelAndView onSubmit(Object cmd,BindException ex) throws Exception

         {

                   Login login =(Login)cmd ;

                   System.out.println(login.getUsername());

                   if(isEntity(login))

                   {

                            HashMap hm = new HashMap();

                            hm.put("login", login);

                            return new ModelAndView(this.getSuccess(),hm);

                   }else

                   {

                            return new ModelAndView(this.getError());

                   }

         }

         private boolean isEntity(Login login)

         {

                   if(login.getUsername() != null && !login.getUsername().trim().equals("") && login.getPassword() !=null && !login.getPassword().trim().equals(""))

                            return true ;

                   else

                            return false ;

         }

         public String getError() {

                   return error;

         }

         public void setError(String error) {

                   this.error = error;

         }

         public String getSuccess() {

                   return success;

         }

         public void setSuccess(String success) {

                   this.success = success;

         }

        

}

你可能感兴趣的:(ping)