Spring&SpringMVC&MyBatis
新建项目,和之前一样,点我查看之前的配置
开始之前要先完成之前spring和mybatis的配置,包括jar包,等等等等…
由于springMVC是spring的一部分,所以配置会很简单
springmcv-config.xml
这个配置在config源文件下
里面就是配置springmvc包扫描,加载注解驱动,视图解析器
代码:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 配置包扫描器,扫描@Controller注解的类 -->
<context:component-scan base-package="heyblack.controller"></context:component-scan>
<!-- 加载注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
这里主要配置spring文件监听器,springmvc编码过滤器,spring前端控制器
代码:
<!-- 配置加载Spring文件的监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器 -->
<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>*.action</url-pattern>
</filter-mapping>
<!-- 配置Spring MVC前端核心控制器 -->
<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:springmvc-config.xml</param-value>
</init-param>
<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--/:拦截所有请求(除了jsp)-->
<url-pattern>/</url-pattern>
</servlet-mapping>
测试
怎么算整合成功呢?
用之前整合spring和mybatis的代码,发布项目后在前端展示出本来打印在后台的数据就完成了。
首先service类
package heyblack.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import heyblack.dao.AccountMapper;
import heyblack.po.Account;
@Service
public class AccountService {
@Autowired
private AccountMapper am;
public Account findAccountByID(Integer id) {
return this.am.findAccountByID(id);
}
}
controller类:
package heyblack.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import heyblack.po.Account;
import heyblack.service.AccountService;
@Controller
public class AccountController {
@Autowired
private AccountService as;
@RequestMapping("/findAccountByID")
public String findAccountByID(Integer id, Model model) {
Account a = as.findAccountByID(id);
model.addAttribute("account", a);
return "first";
}
}
前端视图:
注意这里的路径,是在WEB-INF/jsp下的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>money</td>
</tr>
<tr>
<td>${account.id}</td>
<td>${account.name}</td>
<td>${account.money }</td>
</tr>
</table>
</body>
</html>
打包到服务器运行,看看结果:
完事了