前面面已经完成了2大框架的整合,SpringMVC的配置文件单独放,然后在web.xml中配置整合。
1.配置spring-mvc.xml

<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-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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 扫描带Controller注解的类 -->
<context:component-scan base-package="com.fanwei.myssm.controller" />
<!-- 加载注解驱动 -->
<mvc:annotation-driven/>
<!-- 配置视图解释器 jsp -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

2.配置web.xml文件
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<!-- 上下文的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring/applicationContext.xml</param-value>
</context-param>
<!-- Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- POST提交过滤器 UTF-8 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

<!-- POST提交过滤器 UTF-8 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

4.编写service
目录结构如下:

代码如下:
UserService
public interface UserService {

public User findById(Integer id) throws Exception;
}

UserServiceImpl
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;

@Override
public User findById(Integer id) throws Exception{
return userDao.findUserById(id);
}
}

注意:不要忘记在applicationContext.xml 添加扫描包,加载service

<!--扫描service-->
<context:component-scan base-package="com.fanwei.myssm.service"/>

5. 编写Controller
@Controller
public class ssmController {
@Resource
private UserService userService;

@RequestMapping(value="/login")
public String HelloWorld(Model model){
return "login";
}

@RequestMapping(value = "/user/login")
public String login(Integer id,Model model){
User user = null;
try{
user = userService.findById(id);
}catch (Exception e){
e.printStackTrace();
}
model.addAttribute("username",user.getUsername());
model.addAttribute("sex",user.getSex());
return "success";
}
}

login.jsp中代码
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<body>
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Horizontal Form</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form class="form-horizontal" action="/user/login" method="post" id="registerForm">
<div class="box-body">
用户Id <input id="id" name="id">
</div>
<!-- /.box-body -->
<div class="box-footer">
<button id = "submit" type="submit">根据id查询用户信息</button>
</div>
<!-- /.box-footer -->
</form>
</div>
</body>
</html>

success.jsp中代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<h1>用户名:${username}</h1>
<h1>性别:${sex}</h1>
<title>查询成功</title>
</head>
<body>

</body>
</html>

注意:
在引入Springmvc以后加载文件时需要在文件全限定名前面加上classpath:否则会报

HTTP Status 500 - Servlet.init() for servlet springmvc threw exception

整个项目结构图

从0开始整合SSM框架--3.整合SpringMvc的更多相关文章

  1. 整合SSM框架必备基础—SpringMVC(下)

    在上一篇文章<整合SSM框架必备基础-SpringMVC(上)>中,胖达介绍了关于SpringMVC的诞生.优势以及执行流程等理论知识点,这篇文章打算在实操中加深一下对SpringMVC的 ...

  2. Maven整合SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  3. 整合SSM框架必备基础—SpringMVC(上)

    01 MVC概述 在Web系统开发中一般按照视图(View).模型(Model).控制(Controller)三层设计模式进行构建,视图层负责模型数据的渲染,将数据用一定的形式展现给用户:模型层负责监 ...

  4. SSM框架的整合思路&功能实现

    这是我第一篇博客,关于SSM框架的整合思路以及简单功能实现. 首先,最近刚刚学习Spring+SpringMVC+Mybatis,在开发时遇到形形色色的问题,周遭人也为我提供了一些思路,我会一点点整理 ...

  5. shiro权限控制(一):shiro介绍以及整合SSM框架

    shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...

  6. 手把手教你整合SSM框架(基于课工厂+MyEclipse 2017 CI 10)

    步骤1:myeclipse创建项目,导入spring框架 整合思路:因为spring和spring mvc同源,可以无缝整合,故先整合spring+mybatis,然后配置web.xml.spring ...

  7. SSM 框架快速整合实例--学生查询

    一.快速准备 SSM 框架即 Spring 框架.SpringMVC 框架.MyBatis 框架,关于这几个框架的基础和入门程序,我前面已经写过几篇文章作为基础和入门介绍了.对于这 3 个框架还不熟悉 ...

  8. SSM框架快速整合实例——学生查询

    一.快速准备 SSM 框架即 Spring 框架.SpringMVC 框架.MyBatis 框架,关于这几个框架的基础和入门程序,我前面已经写过几篇文章作为基础和入门介绍了.这里再简单的介绍一下: 1 ...

  9. SSM框架——详细整合教程

    SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 1.基本概念   1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Jav ...

随机推荐

  1. Unity&C# SingerMonoManager泛型单例

    管理各种管理器 ///为什么需要单例 ///单例模式核心在于对于某个单例类,在系统中同时只存在唯一一个实例,并且该实例容易被外界所访问: ///避免创建过多的对象,意味着在内存中,只存在一个实例,减少 ...

  2. .net core 2.0 mvc 初步学习

    mvc_study *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impor ...

  3. Amazon新一代云端关系数据库Aurora(下)

    本文由  网易云发布. 作者:郭忆 本篇文章仅限内部分享,如需转载,请联系网易获取授权. 故障恢复 MySQL基于Check point的机制,周期性的建立redo log与数据页的一致点.一旦数据库 ...

  4. 【文文殿下】Manache算法-学习笔记

    Manache算法 定义:是一个判断回文子串的算法,我们结合例题解释: 题目:给定一个长度为 n 的字符串 S,求其最长回文子串 一个字符串是回文的,当且仅当反转后的串与原串完全相等 分析:对于这个题 ...

  5. nosql基本了解

    数据库分为关系型数据库和非关系型数据库nosql,关系型数据库比较常见,此处不再多讲:nosql有key-value存储数据库,比如redis:文档型数据库,比如mongodb:列存储数据库,比如hb ...

  6. JS 返回上一页并刷新,但不用重新加载整个页面(ajax实现)

    需求 有三个页面A.B.C,点击A=>B,点击B=>C,在C中添加内容,点击确定返回到B,此时B页面需重新加载新的内容.再次点击B的返回按钮,希望返回到A而不是C. ===== 2017/ ...

  7. 使用HBuilderX实现打包vue项目成app

    一.准备开发工具 开发工具:HBuilderX 官网地址:http://www.dcloud.io (标准版需要自己安装插件,app开发版已经把app开发常用的插件安装好了,开箱即用,建议使用开发版) ...

  8. c语言求方阵的行列式、伴随矩阵算法

    #include<stdio.h> #include<math.h> #define N 100 //N比输入的阶数大即可 int main() {   int n,a[N][ ...

  9. StarUML使用简明教程

    最近了解到StarUML比较多,所以写一篇教程供大家参考,不足支持,请见谅. StarUML(简称SU),是一种创建UML类图,生成类图和其他类型的统一建模语言(UML)图表的工具.StarUML是一 ...

  10. Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】

    一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...