springboot08-resultCURD

一、登陆页面:

登陆—>首页

在开发期间,模版引擎页面修改以后,想要实时生效

1)禁用模版引擎的缓存

#禁用缓存
spring.thymeleaf.cache=false

2)页面修改完成以后ctrl+F9:重新编译一下

登陆错误消息的显示:

	<!--判断-->
	<!--显示错误信息,如果返回了msg就代表密码错误-->
	<!--th:if判断成功以后 th:text才会生效-->
	<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

3)拦截器机制进行登录检查

@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginUser = request.getSession().getAttribute("loginUser");
        if(loginUser==null){
            //未登录,转到登录页

            request.setAttribute("msg","没有权限,请先登陆");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else {
            //登陆,放行
            return true;
        }
    }	
@Configuration
public class MymvcController extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/ogj").setViewName("success");
    }

    @Bean
    //所有的WebMvcConfigurerAdapter组件都会一起起作用 ,但是 我们一定要用bean注入到容器中
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter(){
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main").setViewName("dashboard");
            }
            //查看可重写的方法:ctrl+o
            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //静态资源, css js
                //springboot已经做好了静态资源映射 不用处理

                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                        .excludePathPatterns("/index.html","/","/user/login");
            }
        };
        return adapter;
    }
}

二、CRUD-员工列表

1)、Result CRUD: CRUD满足Rest风格;

URI:/资源名称/资源标识 HTTP请求方式区分对资源CRUD操作

普通CRUD Result CRUD
select getEmp emp–GET
add addEmp?xxx emp–POST
update updateEmp?id=xxx&xxx=xx emp/{id}–PUT
delete deleteEmp?id=1 emp/{id}–DELETE

2)、实现请求的架构:

请求URI 请求方式
查询所有员工 emps GET
查询某个员工(来到修改页面) emp/id GET
来到添加页面 emp GET
添加员工 emp POST
来到修改页面(查出员工进行信息回显) emp/1 GET
修改员工 emp PUT
删除员工 emp/1 DELETE

3)、员工列表

thymeleaf公共片段的抽取

1、抽取公共片段
<div th:fragment="copy">
    This is a fragment
</div>


2、引入公共片段
<div th:insert="~{footer :: copy}"></div>
~{templatename::selector}:模版名::选择器
~{templatename::fragmentname}:模版名::片段名

3、默认效果:
insert的功能片段在div标签中
如果使用th:insert等属性进行引入,可以不写~{};
行内写法可以加上:[[~{}]];[(~{})];

三种引入片段的th属性:

th:insert:将公共片段整个插入到元素中

th:replcae:将声明引入的元素替换为公共片段

th:include:被引入的片段的内容包含进这个标签中

<footer th:fragment="copy">
	this is a  fragment
</footer>
引入方式
<div th:insert="footer::copy"></div>
<div th:replace="footer::copy"></div>
<div th:include="footer::copy"></div>
效果

还用一种引入方式:给

4)、链接高亮问题

引入片段的时候,可以添加参数,来解决链接高亮问题。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XbibNJNO-1583755679208)(C:\Users\ouguangji\AppData\Roaming\Typora\typora-user-images\image-20200308223206334.png)]

5)、添加员工

<form th:action="@{/emp}" method="post">
						<!--发送put请求修改员工数据-->
						<!--
						1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的)
						2、页面创建一个post表单
						3、创建一个input项,name="_method";值就是我们指定的请求方式
						-->
						<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
						<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">
						<div class="form-group">
							<label>LastName</label>
							<input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}">
						</div>
						<div class="form-group">
							<label>Email</label>
							<input name="email" type="email" class="form-control" placeholder="[email protected]" th:value="${emp!=null}?${emp.email}">
						</div>
						<div class="form-group">
							<label>Gender</label><br/>
							<div class="form-check form-check-inline">
								<input class="form-check-input" type="radio" name="gender" value="1" th:checked="${emp!=null}?${emp.gender==1}">
								<label class="form-check-label"></label>
							</div>
							<div class="form-check form-check-inline">
								<input class="form-check-input" type="radio" name="gender" value="0" th:checked="${emp!=null}?${emp.gender==0}">
								<label class="form-check-label"></label>
							</div>
						</div>
						<div class="form-group">
							<label>department</label>
							<!--提交的是部门的id-->
							<select class="form-control" name="department.id">
								<option th:value="${dept.id}"
										th:each="dept:${depts}"
										th:text="${dept.departmentName}">1</option>
							</select>
						</div>
						<div class="form-group">
							<label>Birth</label>
							<input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}">
						</div>
						<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>
					</form>

提交的数据格式不对:生日:日期;

2017-12-12;2017/12/12;2017.12.12;

日期的格式化:SpringMVC将页面提交的值需要转换为指定的类型;

2017-12-12----Date;类型转换 格式化

默认日期是按照/的方式的;

我们可以在application上配置:

spring.mvc.date-format=yyyy-MM-dd

源码:https://github.com/a2696870402/spring-boot-01-web-result

发布了65 篇原创文章 · 获赞 29 · 访问量 6480

猜你喜欢

转载自blog.csdn.net/qq_41617848/article/details/104760008
08
今日推荐