springMVC(二)web.xml和springmvc-servlet.xml配置

首先是配置web.xml

将请求交给spring的DispatcherServlet处理
代码如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>springmvctest</display-name>
    <!-- 统一编码 -->
    <filter>
        <filter-name>charsetEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>charsetEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 加载/WEB-INF/[servlet-name]-servlet.xml -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

其次配置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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.2.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 
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-4.2.xsd">

    <!-- 扫描路径 -->
    <context:component-scan base-package="com.test.controller" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 配置根视图 -->
    <mvc:view-controller path="/" view-name="index"/>

    <!-- 激活基于注解的配置 @RequestMapping, @ExceptionHandler,数据绑定 ,@NumberFormat ,
    @DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等  -->
    <mvc:annotation-driven />

    <!-- 静态资源配置 -->
    <mvc:resources location="/assets/" mapping="/assets/**"></mvc:resources>

    <!-- 视图层配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

根据DispatcherServlet的完整路径来看,我们需要加入

spring-core-4.x.x.RELEASE.jar
spring-beans-4.x.x.RELEASE.jar
spring-context-4.x.x.RELEASE.jar
spring-aop-4.x.x.RELEASE.jar
spring-expression-4.x.x.RELEASE.jar
spring-web-4.x.x.RELEASE.jar
spring-webmvc-4.x.x.RELEASE.jar

commons-logging-1.1.3.jar

然后开发Controller

写一个helloworld

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/hello")
public class HelloController {
    @RequestMapping(value="/world",method=RequestMethod.GET)
    public String hello(Model model){
        model.addAttribute("msg", "你好spring mvc");
        return "index";
    }
}

最后开发展示层

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>hello</title>
</head>
<body>
<h1>Hello Spring</h1>
${msg }
</body>
</html>

好了打开浏览器访问
http://localhost:8080/springmvctest/hello/world.html

这里写图片描述

项目结构

这里写图片描述


  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
<h3>回答1:</h3><br/>SpringMVCweb.xml配置主要包括以下内容: 1. 配置DispatcherServletweb.xml配置DispatcherServlet,指定它的servlet-name和servlet-class,以及它所处理的请求的url-pattern。 2. 配置ContextLoaderListener 在web.xml配置ContextLoaderListener,指定它的listener-class,以及它所加载的Spring配置文件的位置。 3. 配置字符编码过滤器 在web.xml配置字符编码过滤器,指定它的filter-name、filter-class和encoding。 4. 配置静态资源访问 在web.xml配置静态资源访问,指定它的servlet-name、url-pattern和文件路径。 5. 配置异常处理器 在web.xml配置异常处理器,指定它的error-page和exception-type,以及处理异常的servlet或jsp页面。 6. 配置文件上传 在web.xml配置文件上传,指定它的servlet-name、url-pattern和文件上传的最大大小。 以上是SpringMVCweb.xml配置的主要内容。 <h3>回答2:</h3><br/>Spring MVC是一个基于Java的web框架,它提供了一种轻量级的WEB开发方式。在使用Spring MVC时,我们需要对其进行web.xml配置,以实现相应的功能。 在web.xml中,我们需要将Spring MVC的DispatcherServlet配置Servlet组件,并添加相应的Servlet映射规则。同时,我们还需要配置Spring MVC的上下文环境,并在其中配置Spring MVC的bean以及其他的第三方组件和服务。 下面我们来详细说明Spring MVC的web.xml配置。 1. 配置DispatcherServlet DispatcherServletSpring MVC的核心控制器,用于接收和分发客户端请求。为了配置DispatcherServlet,我们需要在web.xml文件中添加以下条目: ``` <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 其中,Servlet名称(servlet-name)用于标识DispatcherServlet组件,而Servlet类(servlet-class)则指定了DispatcherServlet的Java类。 2. 配置上下文环境 我们需要在web.xml配置Spring MVC的上下文环境。因为Spring框架使用了IoC(Inverse of Control,控制反转)和依赖注入的机制,在这里我们需要指定Spring MVC用来扫描和装载的应用程序上下文。为了配置上下文环境,我们需要添加以下条目: ``` <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` 在这里,我们指定了一个Spring配置文件的位置,使得DispatcherServlet能够找到它。同时,我们还需要添加一个监听器,使得Spring MVC能够在应用程序启动时初始化上下文环境。 3. 配置字符编码过滤器 在Spring MVC中,我们常常需要处理中文字符,因此在web.xml中需要设置字符编码,以保证数据能够正确传输。为了配置字符编码过滤器,我们需要添加以下条目: ``` <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` 在这里,我们使用CharacterEncodingFilter作为过滤器,将编码设置为UTF-8,并强制进行编码转换。 4. 配置视图解析器 Spring MVC的视图解析器用于将处理器(例如控制器)返回的逻辑视图名称转换为Servlet的URL路径。对于视图解析器的设置,我们需要添加以下条目: ``` <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> ``` 这里我们定义了一个名为viewResolver的bean,它作为视图解析器用来解析视图的逻辑名称。prefix属性指定了视图文件所在的文件夹,而suffix属性指定了视图文件的扩展名。 5. 配置文件上传 在web应用中,文件上传是一个常见的需求。Spring MVC提供了丰富的API来实现文件上传。为了配置文件上传,我们需要添加以下条目: ``` <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10240000"/> </bean> ``` 在这里,我们使用了CommonsMultipartResolver来实现文件上传,并设置了最大文件上传的大小为10MB。 以上是Spring MVC的常见web.xml配置。当然,根据应用程序的需求,还可以添加其他的组件,例如拦截器、数据源等。总之,web.xml配置能够为应用程序提供丰富的功能支持,让开发者能够更加高效、便捷地进行开发。 <h3>回答3:</h3><br/>Spring MVC是一种流行的Java Web框架,它使用基于注解的控制器和依赖注入的方式实现Web应用程序的开发。Spring MVC的Web.xml配置文件是其中一个非常重要的配置文件。 在Spring MVC框架中,Web.xml文件具有指定DispatcherServlet和其他Servlet之间的关系的能力。我们可以通过Web.xml文件来指定Spring MVC中的各种组件的配置和部署环境。 通常,Web.xml文件中包含两个Servlet: - DispatcherServlet,用于将请求路由到正确的处理程序并返回响应。DispatcherServlet是整个Spring MVC应用程序的核心部分。 - ContextLoaderListener,用于加载整个应用程序的根Web应用程序上下文。 在Web.xml文件中,我们需要配置这些元素: - Context Param:在应用程序的所有Servlet中,定义有关应用程序环境的参数的全局配置。例如,我们可以使用上下文参数指定应用程序中使用的数据库连接的URL和密码等。 - Servlet:程序中的每个Servlet必须在Web.xml文件中定义。Servlet在Web应用程序中扮演处理HTTP请求的角色。DispatcherServlet是一个Servlet,它接受请求并处理Web应用程序中的所有Servlet。 - Servlet Mapping:Servlet和URL之间的关系必须在Web.xml文件中定义。Servlet映射决定哪个Servlet处理来自特定URL的请求。 - Filter:过滤器是Web应用程序中的组件,它可以用于修改从客户端到服务器的请求或从服务器到客户端的响应。Filter也在Web.xml文件中定义,以便和其他组件建立正确的关系。 总而言之,Web.xml文件是配置Spring MVC的一个必要的指南。在配置Web.xml文件时,需要详细了解各个元素的作用和相互之间的关系。对于开发人员而言,熟悉Web.xml配置是非常重要的一步。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值