zoukankan      html  css  js  c++  java
  • springMVC的配置和使用

     

    springMVC相对于Struts2学习难度较为简单,并且更加灵活轻便.

    第一步:导入jar包

    spring.jar、spring-webmvc.jar、commons-logging.jar、spring-aop.jar、spring-beans.jar、spring-core.jar、spring-context.jar

    第二步:配置web.xml文件

    复制代码
    <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
      <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc-servlet.xml</param-value>
            </init-param>
            <!-- <load-on-startup>1</load-on-startup> -->
      </servlet>
    
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name><!--这个名字要和上面的servlt-name名字一样--!>
          <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    复制代码

    第三步:配置springmvc-servlet.xml

    这个xml配置的名字和位置要和上面web.xml中param-value中的值一致。

    复制代码
    <?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:context="http://www.springframework.org/schema/context"     
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
           http://www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>">
    
        <!-- 启用spring mvc 注解 -->
        <context:component-scan base-package="test.SpringMVC"/>
        <!-- 使用注解时 需要配置--!>  
        <mvc:annotation-driven />
        <!-- 设置使用注解的类所在的jar包 --> 
    <context:component-scan base-package="controller"></context:component-scan>
    <!-- 完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />   
    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />
    </beans>
    复制代码

    第四步:编写处理器Controller

    Controller的位置要在上面配置的context中base-package路径下。

    复制代码
    @Controller
    @RequestMapping("/user")
    public class UserControl {
    
        /**
    *会处理post方式请求的/user
    *将页面传送进来的值放入user中 */ @RequestMapping(method=RequestMethod.POST) public User createUser(User user){ System.out.println(user.getUsername()+"-"+user.getPassword());return user; } }
    复制代码
  • 相关阅读:
    jquery
    模板库
    Luogu P1902 刺杀大使
    8.20模拟赛再次观光记
    Luogu P1122 最大子树和
    Luogu P1470 最长前缀 Longest Prefix
    8.18爆炸记
    Luogu P1388 算式
    Luogu P1103 书本整理
    8.17
  • 原文地址:https://www.cnblogs.com/hechunhang/p/10842410.html
Copyright © 2011-2022 走看看