annotation-driventransaction-manager="transactionManager"/>
测试:
配置完成启动tomcat
导入jar
UserDao设计
Reposistory接口最大接口!
根据目录继承树结构--->F4开发中XxxxDaoextendsJpaRepostory即可
springdata提供一套接口以及实现类编写UserDao继承指定接口即可完成crud
userDao完成!
专门针对dao测试!
UserService设计
设计简单业务方法
注解完成业务层实现
测试业务层:
UserDao扫描
需要配置事务管理器采用springdata提供事务管理器
开启spring注解扫描
测试:
JPQL查询说明
1:
根据方法名称自动生成jpql语句
参照文档
案例1测试业务层+junit
业务层调用userDao:
userDao业务方法必须符合命名规范
测试:
s
2:
命名查询推荐注解name查询要求:
类名.方法名springdata自动扫描实体类找寻注解@NamedQuery(name="xxx")
案例2:
要求使用@NamedQuery注解在目标查询实体类上声明
实体类添加注解springdata自动搜索类名.方法名称找寻对应HQL语句
junit
3:
@Query注解的查询dao方法名称可以自定义
注解中编写JPQL语句==HQL
案例三演示
4:
@Query注解完成本地sql查询需要添加nativeQuery=true告知springdata书写语句本地sql
5:
参数注入查询@Param注解指定参数类似HQL占位符@Param("lastname')自动将参数值赋予:
lastname
springdatajpa查询结束!
webaction抽取
通用Action模板
封装通用性代码BaseAction
packagecn.itcast.mavenbos.web.action.base;
importjava.lang.reflect.ParameterizedType;
importjava.lang.reflect.Type;
importorg.apache.struts2.ServletActionContext;
importorg.springframework.beans.factory.annotation.Autowired;
importcn.itcast.mavenbos.service.user.UserService;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
importcom.opensymphony.xwork2.ModelDriven;
importcom.opensymphony.xwork2.util.ValueStack;
//复用Action代码
publicabstractclassBaseActionextendsActionSupportimplementsModelDriven{
//各种业务层对象
@Autowired
privateUserServiceuserService;
//@Autowired
//privateUserServiceuserService;
//@Autowired
//privateUserServiceuserService;
//@Autowired
//privateUserServiceuserService;
//@Autowired
//privateUserServiceuserService;
//@Autowired
//privateUserServiceuserService;
//@Autowired
//privateUserServiceuserService;
//@Autowired
//privateUserServiceuserService;
//@Autowired
//privateUserServiceuserService;
//@Autowired
//privateUserServiceuserService;
protectedTmodel;//各个action封装对象model
publicTgetModel(){
returnmodel;
}
//后续子类XxxxAction请求参数封装model里面
publicBaseAction(){
//对model进行实例化,通过子类类声明的泛型
Typesuperclass=this.getClass().getGenericSuperclass();
//转化为参数化类型
ParameterizedTypeparameterizedType=(ParameterizedType)superclass;
//获取一个泛型参数
ClassmodelClass=(Class)parameterizedType.getActualTypeArguments()[0];
try{
model=modelClass.newInstance();
}catch(InstantiationExceptione){
e.printStackTrace();
}catch(IllegalAccessExceptione){
e.printStackTrace();
}
}
//获取请求参数
publicStringgetParameter(Stringname){
returnServletActionContext.getRequest().getParameter(name);
}
//获取SessionAttribute
publicObjectgetSessionAttribute(Stringname){
returnServletActionContext.getRequest().getSession().getAttribute(name);
}
//向session保存属性
publicvoidsetSessionAttribute(Stringname,Objectvalue){
ServletActionContext.getRequest().getSession().setAttribute(name,value);
}
//值栈对象的获取
publicValueStackgetStack(){
returnActionContext.getContext().getValueStack();
}
//压入数据到值栈
publicvoidpush(Objectobj){
getStack().push(obj);
}
//压入数据到值栈
publicvoidset(Stringkey,Objectvalue){
getStack().set(key,value);
}
//分页请求属性驱动
protectedintpage;//页码
protectedintrows;//每页记录数
publicvoidsetPage(intpage){
this.page=page;
}
publicvoidsetRows(introws){
this.rows=rows;
}
}
注入各种业务层接口
login.jsp页面设计
登陆按钮--->同步请求不能提交表单
将标签超链接变成js点击事件--->函数方法--->提交表单
编写jsgo方法实现提交表单
form表单的设计
验证码ajax校验
js完成!
varcheckcode_flag=false;
//页面加载完成进行验证码校验....离焦事件
$(function(){
$("input[name='checkcode']").blur(function(){
//发送ajax请求
$.post("${pageContext.request.contextPath}/user/userAction_validCheckCode",{"checkcode":
this.value},function(data){
if(data){
//true验证码正确可以提交表单用户提示
$("#checkcode_sp").html("√");
checkcode_flag=true;
}else{
//false验证码错误不可以提交表单用户提示
$("#checkcode_sp").html("×");
checkcode_flag=false;
}
});
});
});
//表单的提交
functiongo(){
//提交表单
if(checkcode_flag){
$("#loginform").submit();
}
}
2:
action校验方法从session获取验证码json返回true/false
测试:
ok
login.jsp表单action地址
login业务方法编写
1:
login.jspaction
2:
login业务方法
//登陆业务方法
//登陆业务
@Action(value="userAction_login",results={@Result(name="login_ok",type="redirect",location="/index.jsp"),@Result(name="login_error",location="/login.jsp")})
publicStringlogin(){
Stringsession_code=(String)getSessionAttribute("key");
Stringinput_code=getParameter("checkcode");
if(StringUtils.isNotBlank(inpu