前面面已经完成了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

整个项目结构图

最新文章

  1. ofbiz定时任务配置
  2. [网络安全] [视频分享]KaLi Linux基础培训2016 最新的哦【福吧资源网】
  3. 【Duke-Image】Week_3 Spatial processing
  4. lesson32 Shopping for food
  5. mvc4 分离Controller 出现 未找到路径“/”的控制器或该控制器未实现 IController
  6. MVC路由配置
  7. HDU 1232 并查集/dfs
  8. javascript 网页运行代码效果
  9. WordPress 插件机制的简单用法和原理(Hook 钩子)
  10. 【secureCRT】永久设置背景色和文字颜色
  11. 转__Android开源项目(三 完结篇)
  12. Asp.Net验证码1
  13. 2.Sprng-IoC-Java反射例子
  14. 用JAX-WS在Tomcat中公布WebService
  15. IOS各类问题
  16. 网关、DNS、路由器区别
  17. PHP随手记1--内置函数date
  18. py-oauth2包使用简记
  19. popup_layer插件示例
  20. 一步一步安装SQL Server 2017

热门文章

  1. cesium编程入门(八)设置材质
  2. jmeter5.1.1启动提示not able to find java executable or version的解决办法
  3. C# RDLC报表不出现预览窗体直接输出到打印机
  4. 第八届蓝桥杯JavaB---承压计算
  5. Django 项目拆分配置文件settings.py
  6. 《快学Scala》第一章 基础
  7. 我从Linux走来,选择了Windows
  8. Https 客户端与服务器交互过程梳理
  9. ReactNative常用组件库 react-native-camera 相机
  10. 【GIS新探索】算法实现在不规则区域内均匀分布点