1.springmvc入门程序

spring web mvc 属于表现层框架,是spring框架的一部分。

1.开发环境

Jdk:1.8.0_181

Tomcat:apache-tomcat-7.0.85

Spring:4.2.0

2.开发步骤

  1)创建javaWeb项目

 2)导入jar包

项目右击-->properties-->Deployment Assembly-->add-->Java Build Path Entries-->导入所有依赖的Jar包

 3)配置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" 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>springMvc</display-name>
  <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>
  <!-- springMvc前端控制器 -->
  <servlet>
     <servlet-name>springMvc</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- 指定springMvc核心文件配置位置 -->
       <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:springMvc.xml</param-value>
       </init-param>
     <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

指定springMvc前端控制器,加载springMvc核心DispatcherServlet类,在初始化阶段引入SpringMvc.xml核心配置文件,设置在tomcat启动时加载此servlet。

此映射为前面servlet的mapping,加载路径为所有含有@Controller标签的类。

 4)配置springMvc.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- 配置注解扫描 -->
    <context:component-scan base-package="controller"></context:component-scan>
    </beans>

配置注解扫描,当tomcat启动的时候,就会运行到springMvc.xml,然后该文件就会扫描controller包,看是否有相应注解。

 5)编写pojo类和注解controller类

(1)pojo类

 2)controller

其中@RequestMapping就是映射servlet-mapping配置的*.action。

在之前我们是使用request的getRequestDispatcher。

 6)创建index.jsp显示界面

 7)测试运行

猜你喜欢

转载自blog.csdn.net/qq_37909508/article/details/81205817