一个简单的SpringBoot网页交互和注解的详细介绍(2)

        为了实现网页中一些功能的切换我们使用接口重写的方式进行页面之间的改变,这里主要实现以下几个接口

        1、这个接口是实现登入权限问题的接口,其中loginout是在页面中设置的返回信息,我们可以通过这个值来确定网页是否进行了登入,如果没有登入我们遍不可以放行,返回登入界面,如果登入成功我们便可以进入。

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginhanderInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginout = request.getSession().getAttribute("loginout");
        if(loginout==null){
            request.setAttribute("msg","没有权限,请登入");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }
        else{
            return true;}
    }


}

       

        2、设置网页语言参数,将其设置为英文或者中文,这里需要创建几个配置文件 i18n是国际化的缩写,然后我们在页面中传回来一个参数进行设置,随后按照返回的信息读取配置文件,其中用()表示l的参数名,然后分割符,随后设置默认值,如果不符合默认值就按照分隔符之后的进行填写。

public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //请求语言参数
        String language = request.getParameter("l");
        //ctrl+alt+v
        //没有就是用默认的
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(language)){
            String[] s = language.split("_");
            locale = new Locale(s[0], s[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}

        这里我们重写了addViewControllers方法我们可以在这里人工登入页面的方式,我们可以给他们起一个别名,也就是一个路径名称,下一个重写名称时进行限制,限制不能登入便不可直接登入,但是一些特定网页除外,自己可以设置。@bean表示注入实现接口。

@Configuration
//@EnableWebMvc 不要随便加
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main.html").setViewName("dashboard");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginhanderInterceptor()).addPathPatterns("/**").excludePathPatterns("/list.html","/index.html","/","/user/login","/css/*","/js/*","/img/*");
    }

    //必须要用这个方法
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

 

        注意:这里文件路径要正确,一定是自己的resource路径的Static路径下的文件,然后我们通过/来读取文件路径,这里使用Thymeleaf路径的时候要使用变量地址,其次要在变量前面加上th:,路径使用@{} 变量使用#{}

         3、这里我们有个登入成功与否的姐买你,我们使用RequestMapping进行路径设置,当网页跳转到这个界面时候我们便进入以下方法,其中@RequesrParam表示取表单中的数据,Model表示,HttpSession session.setAttribute用来设置变量的,将一些参数传入到网页中,然后将网页进行重定向redirct会换一个地址输出跟符合逻辑。而model.addAttribute方法可以向前端传递参数,然后可以在前端中${}取到里面的值

@Controller
public class LoginController {
    @RequestMapping("/user/login")
    //@ResponseBody跳一个视图不加这个 语句的话加上
    public String login(@RequestParam("usename") String username, @RequestParam("password") String password
    , Model model, HttpSession session){
        if(!StringUtils.isEmpty(username)&&"123456".equals(password)) {
            session.setAttribute("loginout", username);
            //重写路径
            return "redirect:/main.html";
        }
        else{
            model.addAttribute("msg","用户名或者密码错误");
            return "index";
        }


    }
}

        4、查询数据库中的所有元素可以采用以下方式,将查询结果放入到一个Collection集合中然后通过数据返回。这里需要进行一定的主注入操作,然后通过model传到前端,最后进行一个页面挑战命令。


@Controller
public class employeeController {
    @Autowired
    EmployeeDao employeeDao;
   @RequestMapping("/emps")
    public String setAll(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emp",employees);
        return "list";

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值