• 从0开始整合SSM框架--3.整合SpringMvc


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

    整个项目结构图

  • 相关阅读:
    400
    Eclipse中的Link with Editor功能是如何实现
    SET NOCOUNT
    response的响应头相关方法
    response发送状态码
    myeclipse在开发前的一些设置
    网站访问量统计案例
    使用ServletContext获取相关资源的方法
    获取web.xml文件中初始化参数
    域对象
  • 原文地址:https://www.cnblogs.com/red-evil/p/10057323.html
Copyright © 2020-2023  润新知