Spring MVC入门Demo
1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为springmvcdemo的Maven工程。
2 文件目录结构如下图所示
3 pom.xml中的完整内容为
- <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.0</modelVersion>
- <groupId>springmvcdemo</groupId>
- <artifactId>springmvcdemo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>springmvcdemo</name>
- <description />
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>3.2.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>3.2.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-api</artifactId>
- <version>7.0</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.glassfish.web</groupId>
- <artifactId>javax.servlet.jsp.jstl</artifactId>
- <version>1.2.2</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.2</version>
- <configuration>
- <version>3.1</version>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
4 web.xml中的完整内容为
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- id="WebApp_ID" version="3.1">
- <display-name>springmvcdemo</display-name>
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
5 applicationContext.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <context:component-scan base-package="com.demo" />
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/views/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
6 Student.Java中的完整代码为
- package com.demo.model;
- import java.io.Serializable;
- public class Student implements Serializable {
- private static final long serialVersionUID = 1L;
- private Integer id;
- private String name;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Student [id=" + id + ", name=" + name + "]";
- }
- }
7 StudentDao.java中的完整代码为
- package com.demo.dao;
- import java.util.List;
- import com.demo.model.Student;
- public interface StudentDao {
- public List<Student> getAllStudents();
- }
8 StudentDaoImpl.java中的完整代码为
- package com.demo.dao;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.stereotype.Repository;
- import com.demo.model.Student;
- @Repository
- public class StudentDaoImpl implements StudentDao {
- public List<Student> getAllStudents() {
- List<Student> students = new ArrayList<Student>();
- Student stu1 = new Student();
- stu1.setId(1);
- stu1.setName("Zhang San");
- students.add(stu1);
- Student stu2 = new Student();
- stu2.setId(2);
- stu2.setName("Li Si");
- students.add(stu2);
- return students;
- }
- }
9 StudentManager.java中的完整代码为:
- package com.demo.service;
- import java.util.List;
- import com.demo.model.Student;
- public interface StudentManager {
- public List<Student> getAllStudents();
- }
10 StudentManagerImpl.java中的完整代码为:
- package com.demo.service;
- import java.util.List;
- import org.springframework.stereotype.Service;
- import org.springframework.beans.factory.annotation.Autowired;
- import com.demo.dao.StudentDao;
- import com.demo.model.Student;
- import com.demo.service.StudentManager;
- @Service
- public class StudentManagerImplimplements StudentManager{
- @Autowired
- StudentDao dao;
- publicList<Student> getAllStudents() {
- return dao.getAllStudents();
- }
- }
11 StudentController.java中的完整代码为:
- package com.demo.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.ui.Model;
- import com.demo.service.StudentManager;
- @Controller
- @RequestMapping("/student-module")
- public class StudentController {
- @Autowired
- StudentManager manager;
- @RequestMapping(value = "/getStudentInfo", method = RequestMethod.GET)
- public String getStudentInfo(Model model) {
- model.addAttribute("students", manager.getAllStudents());
- return "showStudentInfo";
- }
- }
12 showStudentInfo.jsp中的完整代码为:
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
- <html>
- <head>
- <title>Spring MVC Demo</title>
- </head>
- <body>
- <h2>All Students</h2>
- <table border="1">
- <tr>
- <th>Student Id</th>
- <th>Student Name</th>
- </tr>
- <c:forEach items="${students}" var="student">
- <tr>
- <td>${student.id}</td>
- <td>${student.name}</td>
- </tr>
- </c:forEach>
- </table>
- </body>
- </html>
13 将springmvcdemo添加进Tomcat7中并运行
在浏览器中输入
http://localhost:8080/springmvcdemo/student-module/getStudentInfo
显示结果为
14 源码下载地址
CSDN:http://download.csdn.net/detail/haishu_zheng/9531315
Github:https://github.com/zhenghaishu/SpringMVC-Demo
Spring MVC入门Demo的更多相关文章
- Spring Mvc 入门Demo
1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...
- 【转】spring MVC入门示例(hello world demo)
部分内容来自网络:<第二章 Spring MVC入门 —— 跟开涛学SpringMVC > 1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web M ...
- Spring MVC 入门教程示例 (一)
今天和大家分享下 Spring MVC 入门教程 首先还是从 HelloWorld web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...
- Java基础-SSM之Spring MVC入门篇
Java基础-SSM之Spring MVC入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Spring MVC简介 1>.什么是Spring MVC 答:Sprin ...
- Spring框架学习(7)spring mvc入门
内容源自:spring mvc入门 一.spring mvc和spring的关系 spring mvc是spring框架提供的七层体系架构中的一个层,是spring框架的一部分,是spring用于处理 ...
- SSM(spring mvc+spring+mybatis)学习路径——2-1、spring MVC入门
目录 2-1 Spring MVC起步 一.回顾Servlet 二.SpringMVC简介 三.搭建SpringMVC第一个案例 四.简单流程及配置 五.使用注解开发Controller 六.参数绑定 ...
- Spring MVC入门的实例
作为Spring MVC入门,以XML配置的方式为例.首先需要配置Web工程的web.xml文件. 代码清单14-1:web.xml配置Spring MVC <?xml version=&q ...
- 1.【转】spring MVC入门示例(hello world demo)
1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...
- Spring MVC 入门示例讲解
在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...
随机推荐
- canvas 进入游戏点击时苹果手机为什么会闪
canvas 进入游戏点击时苹果手机为什么会闪 ?? 大神门 谁有解决办法???
- Linux:hping高级主机扫描
https://www.aliyun.com/jiaocheng/167107.html https://blog.csdn.net/weixin_39762926/article/details/7 ...
- OpenFalcon-SuitAgent
http://www.cnblogs.com/qianlong2016/archive/2016/09/08/5853899.html
- 201. Non Absorbing DFA
题意好难看懂的说... 有限状态自动机DFA是这么一个有序组<Σ, U, s, T, phi>:Σ代表输入字符集,表示此自动机的工作范围:U代表所有的状态集合:s是初始状态:T是最终状态: ...
- AC日记——小魔女帕琪 洛谷 P3802
小魔女帕琪 思路: 概率公式计算: 代码: #include <bits/stdc++.h> using namespace std; ],sig; int main() { ;i< ...
- nodejs安装sharp出错的问题
PS D:\report\source\lpd-planning-allocation> yarn yarn install v1.3.2 [/] Resolving packages... [ ...
- MS SQL Server迁移至Azure SQL
SQL Server的数据目前是存在于公司服务器的,现时需要将它迁移至Azure SQL 迁移分两种 数据库结构复制 数据库结构复制与数据迁移至Azure SQL 第1种方法针对的是将现有数据库创建新 ...
- 【Nginx】初试反向代理:反向代理的原理和用途
Nginx是一个轻量级的服务器,是一个俄罗斯的开发者开发的开源软件.Nginx具有占内存小.并发能力高的特点,底层采用epoll(Linux2.6+)和kqueue(FREEBSD)网络I/O模型,相 ...
- hdu 4825(Trie)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- 一步一步搭建springCloud
一.spring cloud简介Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...