创建springmvc的步骤

本文详细介绍了如何配置 Spring MVC 的 web.xml 和 springmvc-servlet.xml 文件,并通过实例展示了如何创建控制器处理请求并返回视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)先配置好web.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

   <!--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>

      <url-pattern>/</url-pattern>

  </servlet-mapping>

</web-app>

(2)我配置的是在src路径下去找的springmvc-servlet.xml文件。因此,创建在src下边创建springmvc-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.1.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    

 

   <!--  激活标签,生命周期的管理-->

    <context:annotation-config/>

    <!-- dispatcherServlet去在这里搜索com.hmy.ssh.Controller包下边的东西控制器和请求联系-->

    <context:component-scan base-package="com.hmy.ssh.Controller"/>

    <!-- don't handle the static resource -->

    <mvc:default-servlet-handler />

    

    <!-- 默认的注解映射的支持,if you use annotation you must configure following setting  hadlermapping必须用这个-->

    <mvc:annotation-driven />

    <!-- 视图解释类configure the InternalResourceViewResolver,告诉用哪个viewresolver获取view -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 

            id="internalResourceViewResolver">

        <!-- 前缀 ,jsp文件在当前根目录下。可指定路径-->

        <property name="prefix" value="/" />

        <!-- 后缀 -->

        <property name="suffix" value=".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  --> 

    </bean>  

    <!-- 可能这个标签的真谛就是为了引用资源的访问不会类似CONTROLLER一样被拦截,区分出关注的资源的访问,

    一般我们在springMVC里面的拦截都会配置为"/",拦截所有的。  -->

     <mvc:resources location="/images/" mapping="/images/**"/>  

 <mvc:resources location="/js/" mapping="/js/**"/> 

 <mvc:resources location="/css/" mapping="/css/**"/>

 <mvc:resources location="/data/" mapping="/data/**"/>  

</beans>

 

<!-- 拦截器 -->  

<!--

   <mvc:interceptors>  

    <bean class="com.hmy.ssh.MyInteceptor" />  

</mvc:interceptors>   -->

(3)创建controller类开始处理

package com.hmy.ssh.Controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

//告诉mvc是一个controller

@RequestMapping("/hello")

//拦截根目录下的hello

public class MyTestController {

@RequestMapping("/mvc")

//localhost:8888/hello/mvc

//即可,映射去到了home,代表了home.jsp

public String HelloMvc(){

return "jsp/home";//这里,如果是webroot下边的jsp文件夹下边的jsP文件,则要写明路径,否则404错误。

}

}

(4)打开网页,成功跳转

http://localhost:8888/CourseOnline/hello/mvc


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值