springMVC部署
⼀、导⼊springMVC所需要的jar包
  下载地址:repo.spring.io/release/org/springframework/spring/
⼆、springMVC框架配置
  1、
1.1 常规l配置
1<servlet>
2<servlet-name>spring</servlet-name>
3<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4<load-on-startup>1</load-on-startup>
5</servlet>
6<servlet-mapping>
7<servlet-name>spring</servlet-name>
8<url-pattern>*.do</url-pattern>
9</servlet-mapping>
      1.2 spring配置⽂件不在WebRoot/WEB-INF下创建时,l ⽂件配置:
<!-- spring mvc 核⼼servlet -->
<servlet>
<servlet-name>applicationContext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载相应的xml,⽽不会去加载/WEB-INF/下的l。 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>springmvc考试选择题
<servlet-name>applicationContext</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
  2、(在WebRoot/WEB-INF下)创建spring配置⽂件
  注意:此配置⽂件名称必须为 :l  (servletName即为l中核⼼servlet的name),此处名称为l。如果不在WebRoot/WEB-INF下创建,则需要在l中作相应配置,否则⽂件不到,此时l配置参考 1.2 spring配置⽂件不在WebRoot/WEB-INF下创建时,l ⽂件配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:aop="/schema/aop"
xmlns:mvc="/schema/mvc"
xmlns:context="/schema/context"
xmlns:tx="/schema/tx"
xsi:schemaLocation="
/schema/aop
/schema/aop/spring-aop-3.0.xsd
/schema/mvc
/schema/mvc/spring-mvc-3.0.xsd
/schema/beans
/schema/beans/spring-beans-2.5.xsd
/schema/context
/schema/context/spring-context-3.0.xsd
/schema/tx
/schema/tx/spring-tx-3.0.xsd">
<!--1 MVC注解⽀持  -->
<mvc:annotation-driven />
<!--2 注解扫描⽬录-->
<context:component-scan base-package="com.sxdx"></context:component-scan>
<!--3 处理静态⽂件 -->
<mvc:default-servlet-handler />
<!-- 视图解析器 -->
<!-- <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">            <property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
</bean> -->
</beans>
  3、创建Controller控制器(Action) 
1package com.sxdx.hello;
2import javax.servlet.http.HttpServletRequest;
3import javax.servlet.http.HttpServletResponse;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.servlet.ModelAndView;
7
8 @Controller
9public class demo {
10    @RequestMapping(value="/demo1")
11public String demo1(HttpServletRequest request,
12                HttpServletResponse response){
13        System.out.println("demo1===");
14return "";
15    }
16 }
  4、浏览器访问 localhost:8080/spring01/demo1.do 后台输出demo1===

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。