spring mvc入门案例

spring mvc入门案例

1.步骤

  • 新建web动态过程
    spring mvc入门案例_第1张图片
    2.加入spring jar包

  • commons-logging-1.2.jar

  • spring-aop-4.2.4.RELEASE.jar
  • spring-beans-4.2.4.RELEASE.jar
  • spring-context-4.2.4.RELEASE.jar
  • spring-core-4.2.4.RELEASE.jar
  • spring-expression-4.2.4.RELEASE.jar
  • spring-web-4.2.4.RELEASE.jar
  • spring-webmvc-4.2.4.RELEASE.jar
  • spring-aspects-4.2.4.RELEASE.jar
  • spring-context-support-4.2.4.RELEASE.jar
  • spring-jms-4.2.4.RELEASE.jar
  • spring-tx-4.2.4.RELEASE.jar
  • spring-jdbc-4.2.4.RELEASE.jar
  • spring-messaging-4.2.4.RELEASE.jar

3.在web.xml中配置DispatchServlet

配置 DispatcherServlet :DispatcherServlet 默认加载 /WEBINF/.xml 的 spring 配置文件, 启动 WEB 层的 Spring 容器。可以通过 contextConfigLocation 初始化参数自定义配置文件的位置和名称


<servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

    
    <init-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:springmvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
servlet>

<servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>*.shtmlurl-pattern>
servlet-mapping>

4.加入springmvc的配置文件


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

    
    <context:component-scan base-package="com.wowo.controller" />

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    bean>

beans>

5.控制器代码实现

@Controller
public class UserController {
    /***
     * 第一个入门案例
     * @return
     */
    @RequestMapping(value = "/user/hello")
    public String hello(){
        System.out.println("这是我的第一个入门案例......");
        return "/WEB-INF/hello.jsp";
    }
}

6.运行效果
spring mvc入门案例_第2张图片

你可能感兴趣的:(spring-mvc,spring,mvc,入门案例)