目录
SpringMVC入门学习(三)
/    

SpringMVC入门学习(三)

Spring MVC入门学习(三)

重定向与转发

   这里不再做过多的叙述, 在学JavaWeb都有讲过.

  • 转发: 是服务器行为, 一次请求, (地址栏不变化)在视图层可以使用当前请求Request域的数据, 转发路径必须是同一个web容器下的url.
  • 重定向: 是客户端行为, 两次请求, (地址栏有变化) , 可以重定向到任意的URL
@RequestMapping("/register")
public String register(){
    return "register";  // 转发到register.jsp
}
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMappng("/index")
public class IndexController{
    
    @RequestMapping("/login")
    public String login(){
        // 转发到一个请求方法, (同一个控制器类中可以省略/index/)
        return "forward:/index/isLogin";
    }
    
    @RequestMapping("/isLogin")
    public String isLogin(){
        // 重定向到一个请求方法
        return "redirect:/index/isRegister";
    }
    
    @RequestMapping("/isRegister")
    public String isRegister(){
        // 转发到一个视图
        return "register";
    }
   
}

  

   在SpringMVC框架中, 不管是重定向或转发, 都需要符合视图解析器的配置, 如果直接转发到一个不需要DispatcherServlet的资源. 例如:

return "forward:/html/my.html";

则需要使用mvc:resources配置

<mvc:resources locaton="/html/" mapping="html/**"></mvc:resources>

使用@Autowired进行依赖注入

   在Spring MVC中, 为了能作为依赖注入, 类必须使用org.springframework.stereotype.Service 注解类型表明为@Service(一个服务). 另外, 还需要在配置文件中使用<context:component base-package="基本包"/>元素来扫描依赖基本包

例子

  • 将之前的例子, 分离出service层
package cn.lacknb.service;

import cn.lacknb.beans.UserForm;

public interface UserService {

    boolean login(UserForm userForm);
    boolean register(UserForm userForm);

}

package cn.lacknb.service;

import cn.lacknb.beans.UserForm;
import org.springframework.stereotype.Service;

// 注解为一个服务
@Service
public class UserServiceImpl implements UserService {
    @Override
    public boolean login(UserForm userForm) {
        if ("zhangsan".equals(userForm.getUname()) && "123456".equals(userForm.getUpass())){
            return true;
        }
        return false;
    }

    @Override
    public boolean register(UserForm userForm) {
        if ("zhangsan".equals(userForm.getUname()) && "123456".equals(userForm.getUpass())){
            return true;
        }
        return false;
    }
}

package cn.lacknb.controller;

import cn.lacknb.beans.UserForm;
import cn.lacknb.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    //得到一个用来记录日志对象, 这样在打印信息的时候能够标记打印的是哪个类的信息
    private static final Log loger = LogFactory.getLog(UserController.class);
    // 将服务依赖注入到属性userService
    @Autowired
    public UserService userService;
    /*
    * 处理登录
    * */
    @RequestMapping("/login")
    public String login(UserForm userForm, HttpSession session, Model model){
        if (userService.login(userForm)){
            session.setAttribute("user", userForm);
            loger.info("登录成功");
            return "main";
        }else{
            loger.info("登录失败");
            model.addAttribute("messageError", "用户名或密码错误");
            return "login";
        }
    }

    /*
    * 处理注册
    * */

    @RequestMapping("/register")
    public String register(@ModelAttribute("user") UserForm user){

        if (userService.register(user)){
            loger.info("注册成功");
            return "login";
        }else{
            loger.info("注册失败");
            return "register";
        }

    }

}

@ModelAttribute

  1. 绑定请求参数到实体对象(表单的命令对象), 见前面的例子
  2. 注解一个非请求处理方法

   被@ModelAttribute注解的方法将在每次调用该控制类的请求处理方法前被调用. 这种特性可以用来控制登录权限, 当然控制登录权限的方法有很多. 例如: 拦截器,、过滤器等.

package cn.lacknb.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpSession;

public class BaseController {

    @ModelAttribute
    public void isLogin(HttpSession session, Model model) throws Exception {
        if (session.getAttribute("user") == null){
            throw new Exception("没有权限");
        }
    }
}

package cn.lacknb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/test")
public class ModelAttributeController extends BaseController {

    @RequestMapping("/add")
    public String add(){
        return "add";
    }


    @RequestMapping("/delete")
    public String delete(){
        return "delete";
    }

    @RequestMapping("/modify")
    public String modify(){
        return "modify";
    }

    @RequestMapping("/find")
    public String find(){
        return "find";
    }

}


标题:SpringMVC入门学习(三)
作者:gitsilence
地址:https://blog.lacknb.cn/articles/2019/11/07/1577974161454.html