Sping MVC 入门

来源:互联网 发布:特蕾莎修女知乎 编辑:程序博客网 时间:2024/04/27 04:27

基于极客学院教程的学习笔记

Controller负责数据的接受和页面的跳转,具体的业务逻辑交由service处理
Service负责业务逻辑,发布命令
Dao负责接受命令进行数据处理


配置web.xml和Sping-MVC.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">  <display-name></display-name>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath*:applicationContext.xml</param-value>  </context-param>    <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/spring-mvc.xml</param-value>    </init-param>  </servlet>  <servlet-mapping>    <servlet-name>spring</servlet-name>    <!-- 使用.html作为请求后缀 -->    <url-pattern>*.html</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:util="http://www.springframework.org/schema/util"    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">    <!-- 自动扫描 com.jikexueyuan 包下面的所有组件(使用了springmvc注解)-->    <context:component-scan base-package="com.jikexueyuan.*" /></beans>

Controller

JSP页面通过提交不同的请求进入Controller调用不同的方法
比如:如果请求是login.xml 则调用 toLoginPage()方法

package com.jikexueyuan.demo.springmvc.lesson2.controller;@Controller@SessionAttributes("user")public class UserController {    @Resource    UserService service;    @RequestMapping("login")    public String toLoginPage(){        return "/WEB-INF/jsp/login.jsp";    }    @RequestMapping(value = "doLogin", method = RequestMethod.POST)    public String doLogin(@RequestParam String username , @RequestParam String password, HttpServletRequest request, ModelMap map ){        try {            //具体逻辑交由service处理            User user = service.doLogin(username, password);             /**             * ModelMap map 与 @SessionAttributes("user")配合使用,将user放进session中             */            map.put("user", user);        } catch (Exception e) {            request.setAttribute("error", e.getMessage());            return "/WEB-INF/jsp/login.jsp";        }        return "/WEB-INF/jsp/loginsuccess.jsp";    }    @RequestMapping("doLogout")    public String doLogout(SessionStatus status){        status.setComplete();//清空session中的内容        return "/WEB-INF/jsp/login.jsp";    }}

Service

具体业务逻辑交由service处理

@Servicepublic class UserService {    @Resource    UserDao dao ;    public User doLogin(String username, String passwrod) throws Exception {        if (username == null || "".equals(username)) {            throw new Exception("用户名不能为空");        }        if (passwrod == null || "".equals(passwrod)) {            throw new Exception("密码不能为空");        }               User user = dao.selectByUsername(username);        if (user == null) {            throw new Exception("用户名不存在");        }        if (!user.getPassword().equals(passwrod)) {            throw new Exception("密码错误");        }        return user ;    }}

数据处理层

@Repositorypublic class UserDao {    public User selectByUsername(String username) {        // admin        if ("admin".equals(username)) {            User user = new User();            user.setPassword("123");            user.setUsername("admin");            return user;        }        return null;    }}
0 0
原创粉丝点击