Spring MVC入门之构建到运行,超详细(利用Maven构建)

1、概述

Spring MVC是由Spring提供的一个Web框架,由于其强大而灵活的功能,得到了广泛的应用,因此作为一名程序员的话是必须要掌握的,另外Maven目前也是流行的项目管理工具,因此,本文讲解如何用Maven构建项目,并且详细说明Spring MVC如何从构建到运行。废话不多说,下面开始。

2、项目构建

软件参数

Eclipse:Luna Service Release 2
Tomact:apache-tomcat-8.0.33
Jdk:1.7
Spring-framework:4.2.9.RELEASE

新建工程

左键点击file,选择New,然后选择Mave Project
Spring MVC入门之构建到运行,超详细(利用Maven构建)_第1张图片
点击Next
Spring MVC入门之构建到运行,超详细(利用Maven构建)_第2张图片
选择maven-archetype-webapp,点击Next
Spring MVC入门之构建到运行,超详细(利用Maven构建)_第3张图片
Group Id, Artifct Id,Package这些都是可以随便填的,填完后点击finish即可
Spring MVC入门之构建到运行,超详细(利用Maven构建)_第4张图片
下面先来一张项目新建完后的截图
Spring MVC入门之构建到运行,超详细(利用Maven构建)_第5张图片

项目配置

porm.xml配置

Maven项目建好后,如果想对其进行配置,则通过porm.xml文件进行。
首先指定Maven仓库。这里我用了阿里云的仓库


<repositories>
    <repository>
        <id>publicid>
        <name>aliyun nexusname>
        <url>http://maven.aliyun.com/nexus/content/groups/public/url>
    <releases>
        <enabled>trueenabled>
    releases>
    repository>
repositories>

引入项目依赖

<dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>3.8.1version>
          <scope>testscope>
        dependency>
    <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${springframework-version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>${springframework-version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-context-supportartifactId>
                <version>${springframework-version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aopartifactId>
                <version>${springframework-version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${springframework-version}version>
                <scope>testscope>
            dependency>
            <dependency>
                <artifactId>commons-loggingartifactId>
                <groupId>commons-logginggroupId>
                <version>${commons-logging-version}version>
            dependency>
            
            <dependency>
                <groupId>jstlgroupId>
                <artifactId>jstlartifactId>
                <version>${jstl-version}version>
            dependency>
      dependencies>

porm.xml文件配置好后,下面进行Spring MVC配置

在web.xml(WEB-INF下)文件中加入如下代码


          <servlet>
            <servlet-name>dispatcherservlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            servlet-class>
            <load-on-startup>1load-on-startup>
        servlet>

    <servlet-mapping>
        <servlet-name>dispatcherservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

在WEB-INF文件下新建dispatcher-servlet.xml,写入如下代码


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

        
        <context:component-scan base-package="com.li.demo" />
        
        <mvc:annotation-driven />
        <context:annotation-config /> 

        
        <bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/"/>
        <property name="suffix" value=".jsp"/>
         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

    bean>
beans>

至此Spring MVC配置完毕,下面开始写代码

项目代码

两个jsp页面
userregister.jsp (注册页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<form action="save" method="post">
    <label>姓名:label><input name="name" type="text" id="name">
    <label>邮箱:label><input name="email" type="text" id="email">
    <label>地址:label><input name="address" type="text" id="address">
    <label>电话:label><input name="phone" type="text" id="phone">
    <input type="submit" value="保存" id="submit">
form>
body>
html>

success.jsp(注册成功后信息回显页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <div>姓名:${user.name}div>
    <div>地址:${user.address}div>
    <div>电话:${user.phone}div>
    <div>邮箱:${user.email}div>
body>
html>

下面是java代码
新建User类

public class User {

    private String name;
    private String address;
    private String phone;
    private String email;
    //省略Set Get方法。。。    
}

UserRegisterControler.java

@Controller

public class UserRegisterControler {


    @RequestMapping("/save")
    public String save(@ModelAttribute("form") User user, Model model) {
        model.addAttribute("user", user);
        return "success";
    }
}

至此代码全部写完,下面先来一张项目完成后结构图
Spring MVC入门之构建到运行,超详细(利用Maven构建)_第6张图片

项目运行

将项目部署到Tomcat上后,在浏览器输入http://localhost:8080/mvc/userregister.jsp
这里写图片描述
点击保存后
Spring MVC入门之构建到运行,超详细(利用Maven构建)_第7张图片
可以看到,成功回显了注册信息,项目大功告成!

你可能感兴趣的:(springmvc,maven,spring,mvc,eclipse,maven)