SpringMVC 入门一

来源:互联网 发布:ppt软件学习 编辑:程序博客网 时间:2024/06/06 10:49

步骤:

1、添加Jar包

2、在web.xml文件中配置DispatcherServlet

<!-- 配置DispatcherServlet --><servlet>    <servlet-name>TestSprigMVCServlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- 1, 手动配置初始化参数 -->    <!--配置初始化参数,即配置springmvc的配置文件的路径-->    <!--     <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:springmvc.xml</param-value>    </init-param>     -->     <!-- 2, 使用默认的配置:即只需要将springmvc的配置文件放在WEB-INFO目录下,并且名称必须为<servlet-name>-servlet.xml,( 例如比例中的名称应该为TestSprigMVCServlet-servlet.xml)即可 --></servlet><!-- 配置请求路径的映射 --><servlet-mapping>    <servlet-name>TestSprigMVCServlet</servlet-name>    <!-- 拦截所有请求 -->    <url-pattern>/</url-pattern></servlet-mapping>

3、添加SprinMVC配置文件(TestSpringMVCServlet-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: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-4.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">    <!-- 配置自动扫描的包 -->    <context:component-scan base-package="com.wang.springmvc"></context:component-scan>    <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"/>        <property name="suffix" value=".jsp"/>    </bean></beans>

4、编写处理请求的处理器,并标志为处理器

@Controllerpublic class HelloWorld {    /*     * 1,使用@RequestMapping 注解来映射请求的URL,调用此方法的请求的物理视图路径应该为/WEB/INF/views/helloworld/success.jsp     */    @RequestMapping("/helloworld")    public String hello() {        System.out.println("success");        return "success";    }}

5、编写视图

请求页面

<html><body>    <!-- 请求路径 -->    <a href="helloworld">Hello World!</a></body></html>

在/WEB-INF/目录下添加views目录, 然后添加success.jsp文件即可。

0 0
原创粉丝点击