springMVC配置

1、所需jar包

2、web.xml文件

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>spring</display-name>

      <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-config.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>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

load-on-startup表示启动容器时初始化该Servlet;

url-pattern表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。

自此请求已交给Spring Web MVC框架处理,因此我们需要配置Spring的配置文件,默认DispatcherServlet会加载WEB-INF/配置文件名.xml配置文件

3、spring配置文件

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

    <!--  通知spring容器通过注解的方式装配bean -->
      <context:annotation-config />
    <!--  通知spring容器采用自动扫描机制查找注解的bean -->
      <context:component-scan base-package="com.*" /> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

  InternalResourceViewResolver:用于支持Servlet、JSP视图解析。

viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包。

prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WebRoot/*.jsp”。

3、Java代码

package com.controller;

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

@Controller
public class ProductsController {

    @RequestMapping("/testSpringMVC")
    public String testSpringMVC(String name, Model model){
        System.out.println("name = "+name);
        model.addAttribute("name", name);
        return "success";
    }

}

4、视图页面

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
  </head>

  <body>
    <form action="testSpringMVC" method="post">
          <input type="text" name="name">
        <input type="submit" value="确定">
    </form>  </body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>success</title>
  </head>

  <body>
    ${name},success. <br>
  </body>
</html>
时间: 2024-08-24 07:04:44

springMVC配置的相关文章

SpringMVC配置实例

一.SpringMVC概述 MVCII模式实现的框架技术 Model--业务模型(Biz,Dao...) View--jsp及相关的jquery框架技术(easyui) Contraller--DispatcherServlet以及控制器组件 二.控制器组件 1).DispatherServlet 2).xxx-servlet.xml文件 3).转向模型ModelAndView类 4).相应的Aop组件和拦截器组件 三.springMvc的开发步骤 1.在当前工程引入spring组件包 2.编写

SpringMVC 配置UEditor

SpringMVC 配置UEditor,把UEditor 下的jsp 文件夹放到项目中放jsp页面的目录下,在Controller中中写一个方法(如:@RequestMapping(value="/load/img")访问jsp页面(就跟访问一般的jsp页面一样),需要注意的是,jsp文件夹下的jsp文件和js文件必须放到一起.再把UEditor的后台配置中 URL 中的jsp/controller.jsp改为Controller中的方法的访问URL,就是上面所说的/YourContr

SpringMVC 配置多视图解析器(velocity,jsp)

1.自定义视图解析器 package com.zhaochao.controller; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework

SpringMVC配置web.xml文件详解(列举常用的配置)

常用的web.xml的配置 1.Spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(listener-class) ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默

springmvc 配置过程及详解

加入jar包 在web.xml中 添加spring监听器 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 添加spring容器(父容器)配置文件: <context-param> <param-name>contextConfigLocation</param

springmvc配置之mvc:annotation-driven

为了简化springmvc配置,spring同时引入了mvc namespace, 配置了 <mvc:annotation-driven/> spring会默认注册a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter, and an ExceptionHandlerExceptionResolver 三个bean, 来支持使用注解(@RequestMapping.@ExceptionHandler.@Controller等

Spring实战第七章————SpringMVC配置的替代方案

SpringMVC配置的替代方案 自定义DispatherServlet配置 我们之前在SpittrWebAppInitializer所编写的三个方法仅仅是必须要重载的abstract方法.但还有更多的方法可以进行重载,从而实现额外的配置. 例如customizeRegistration().在AbstractAnnotationConfigDispatcherServletInitializer将DispatcherServlet主车道Servlet容器后,就会调用该方法,并将Servlet注

springMVC配置jsp/html视图解析器

目录 1.maven项目引入freemark相关jar包 2.freemarker.properties 3.配置视图解析器 参考自springMVC配置jsp.html多视图解析器,本文稍作补充 1.maven项目引入freemark相关jar包 freemaker是以个模板引擎,可以根据提供的数据和创建好的模板,去自动的创建html静态页面.所以在返回html视图时可以用这个引擎结合数据生成html静态页面. <dependency> <groupId>org.springfr

FreeMarker学习(springmvc配置)

springMvc配置 <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/templates/"/> <property name=&

详解Springboot中自定义SpringMVC配置

详解Springboot中自定义SpringMVC配置 WebMvcConfigurer接口 ? 这个接口可以自定义拦截器,例如跨域设置.类型转化器等等.可以说此接口为开发者提前想到了很多拦截层面的需求,方便开发者自由选择使用.由于Spring5.0废弃了WebMvcConfigurerAdapter,所以WebMvcConfigurer继承了WebMvcConfigurerAdapter大部分内容. WebMvcConfigurer接口中的方法 举例1:configurePathMatch配置