项目--登录模块控制和视图层

1.登陆页面

 只要是路径就用@

<form class="loginForm"  th:action="@{/login}" method="post">
				
                <div class="inputbox">
                    <label for="user" th:text="#{login.username}">Username</label>
                    <input id="user" type="text" name="username"  required/>
                </div>
                <div class="inputbox">
                    <label for="mima"  th:text="#{login.password}">Password</label>
                    <input id="mima" type="password" name="password"  required/>
                </div>
				<div class="subBtn" >
						<input type="checkbox">[[#{login.remember}]]
                </div>
				<br/>
                <div class="subBtn">
                    <input type="submit" value="Sign"   th:value="#{login.submit}"/>
                    <input type="reset" value="Reset"  th:value="#{login.reset}"/>
                </div>
				<br/>
				<div style="margin-left: 100px;">
                    <a href="#" th:href="@{index.html(l='zh_CN')}">中文</a>
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="" th:href="@{index.html(l='en_US')}" >English</a>
                </div>
            </form>
       

关键-》form中action和method    用户名和密码中的name

2.controller层

“123"写在前面是为了防止空指针异常

若不使用重定向,刷新页面会弹出一个框,让你确定  ;redirect:后面不能有空格,否则浏览器的url会%29%这种样子

package com.cc.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.util.Map;

@Controller
public class LoginController {

    @PostMapping("/login")
    public String login(String username, String password, Map<String,Object>map){
//        判断用户名不为空,且密码为123
          if(!StringUtils.isEmpty(username) && "123".equals(password)){
              //登录成功
              //重定向 redirect: 可以重定向到任何一个请求中(包括其他项目),地址栏改变
//              return "main/index"; //在静态资源中查找文件

              return "redirect:/main.html"; //导航栏地址,不是在静态资源中查找文件,需要视图
          }

//          登陆失败
           map.put("msg","用户名或密码错误");
          return "main/login";
    }
}

  config/ webMvcConfigurer 视图:(Ctrl+D  复制粘贴在下一行)

package com.cc.springboot.config;

import com.cc.springboot.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MySpringMvcConfigurer {
    @Bean
//    跳转首页
    public WebMvcConfigurer webMvcConfigurer() {
      return   new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("main/login");
                registry.addViewController("/index.html").setViewName("main/login");
                registry.addViewController("/main.html").setViewName("main/index");
            }
        };
    }
    @Bean
//    切换语言  自己定义的区域解析器
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值