java web框架学习:spring与springmvc整合

使用的工具:IntelliJ IDEA 2018
(1)新建项目
选择Maven,并勾选模板,Next
java web框架学习:spring与springmvc整合_第1张图片
(2)搭建项目骨架
在main目录下新建如下目录
并将java标记为sources目录,resources标记为resources
java web框架学习:spring与springmvc整合_第2张图片
(3)引入所需依赖

我们只需要引入spring-webmvc就可以了,另外我引入了tomcat的插件,用于运行项目,具体的pom文件如下:



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>com.example.electronic-photo-albumgroupId>
  <artifactId>electronic-photo-albumartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>electronic-photo-album Maven Webappname>
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>4.3.2.RELEASEversion>
    dependency>
  dependencies>

  <build>
    <finalName>electronic-photo-albumfinalName>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <configuration>
          <path>/electronic-photo-albumpath>
          <port>8080port>
        configuration>
      plugin>
    plugins>
  build>
project>

(4)创建spring配置文件applicationContext-beans.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
beans>

(5)创建springmvc配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <mvc:annotation-driven/>
    
    <context:component-scan base-package="com.example.controller"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    bean>
beans>

(6)整合spring和spring mvc配置,在web.xml文件中



<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>
    
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext-*.xmlparam-value>
  context-param>
    
  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>
      <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

(7)测试代码
在spring mvc配置文件中的扫描包路径下新建测试文件HelloController.java

package com.example.controller;

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

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

在欢迎文件index.jsp中增加访问请求的超链接

<html>
<body>
<h2>Hello World!h2>
<a href="hello">helloa>
body>
html>

在配置了视图解析器的路径下新建hello.jsp文件

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/11/17
  Time: 12:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello worldtitle>
head>
<body>
hello world
body>
html>

(8)运行项目
在Maven Projects中运行tomcat插件
java web框架学习:spring与springmvc整合_第3张图片
欢迎页面
java web框架学习:spring与springmvc整合_第4张图片
点击hello超链接,成功跳转
java web框架学习:spring与springmvc整合_第5张图片

你可能感兴趣的:(java,web框架)