1.object类型转为int:
Integer.valueOf(list.get(0).toString());
2.获取input输入中的值:
alert($("#userCode").val());
alert("this is "+$("input[type='text']").attr("value"));
3.action中获取表单数据:
//使用ajax
function checkUserCode(){
/*
1.焦点离开事件绑定
2.获得文本框数据
3.发送ajax请求
*/
var $userCode = $("#userCode").val();
$.post("${pageContext.request.contextPath}/userAction_checkUserCode",{"tempUserCode":$userCode},
function(data){
alert(data);
if(data=="1"){
alert("用户已经存在,请更换注册名~");
$("#checkFlag").val("false");
return;
//用户的注册名存在
}else{
alert("恭喜,可以使用这个注册名~");
$("#checkFlag").val("true");
return;
}
}
)
}
//获取页面传递的数据
private String tempUserCode;
public void setTempUserCode(String tempUserCode) {
this.tempUserCode = tempUserCode;
}
4.js中return:
A return返回null,起到中断方法执行的效果,只要不return false事件处理函数将会继续执行,表单将提交
B return false,事件处理函数会取消事件,不再继续向下执行。比如表单将终止提交。
//是否提交?
function checkSubmit(){
var flag = $("#checkFlag").val();
if(flag=="true"){
return true;
}else{
alert("用户已经存在,请更换注册名~重新提交~");
return false;
}
}
5.表单action属性:
action="${pageContext.request.contextPath}/userAction_login"
6.login.jsp中显示错误信息:
<%@ taglib prefix="s" uri="/struts-tags" %>
<TR>
<TD style="HEIGHT: 28px" width=230 colspan="2">
<s:fielderror value="msg"></s:fielderror>
</TD>
</TR>
7.退出登录,清除session中数据
//退出登录
@Action("userAction_loginout")
public String loginout() {
ServletActionContext.getRequest().getSession().removeAttribute("user");//清除指定的session
System.out.println("userAction_loginout.......========---------");
// ServletActionContext.getRequest().getSession().invalidate();//清除session中的所有东西
return "login";
}
8.模型驱动已经帮我们封装好了数据:
//修改密码
@Action("userAction_edit")
public String edit() {
/*
* session中获取原来的值。模型驱动中,获取新值。
* 调用service进行修改
* 清空session中的数据。
*/
User oldUser = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
//已经被封装到,模型驱动中[封装之前必须保证:输入项的name值=实体类属性的set方法后面的小写开头的属性。]
oldUser.setUserPassword(user.getUserPassword());
userService.update(oldUser);
ServletActionContext.getRequest().getSession().removeAttribute("user");
return SUCCESS;
}
9.jsp页面显示list集合长度:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
list的长度是:${fn:length(baseDictLevelList)}
10.action中保存数据+jsp页面遍历list集合数据:
@Controller
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/")
@Results({
@Result(location="/jsp/customer/add.jsp",name="addUI")
})
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {..类里面的内容如下:页面想要获取数据,不能使用重定向..}
//级别 行业 来源+相应的添加get方法
private List<BaseDict> baseDictLevelList;
private List<BaseDict> baseDictIndstryList;
private List<BaseDict> baseDictSourceList;
@Action("customer_addUI")
public String addUI() {
//调用service查询字典表数据->级别,所属行业,来源
baseDictLevelList =baseDistService.findBaseDictByTypeCode("006");
baseDictIndstryList =baseDistService.findBaseDictByTypeCode("001");
baseDictSourceList =baseDistService.findBaseDictByTypeCode("009");
System.out.println("lsit======="+baseDictSourceList.get(0).getDictItemName());
//数据放到值栈,(Struts的属性封装),为了在页面显示
//把集合数据 放入值栈中--》封装成private成员变量,同时提供get方法,可以自动放入值栈中。
return "addUI";
}
private Customer customer= new Customer();
@Override
public Customer getModel() {
return customer;
}
<select name="cust_level" style="WIDTH: 180px">
<c:forEach items="${baseDictLevelList}" var="baseDictLevel">
<option value="${baseDictLevel.dictId}">${baseDictLevel.dictItemName}</option>
</c:forEach>
</select>
11.按照ID进行查询
public Customer findByCustId(Customer customer) {
// TODO Auto-generated method stub
return (Customer) HibernateTemplate.get(Customer.class,customer.getCustId());
}
12.获取原来下拉框选中的值
action中的代码:
//获取查询的数据库数据
Customer tempCustomer = customerService.findByCustId(customer);
//显示单条数据
ActionContext.getContext().getValueStack().set("tempCustomer", tempCustomer);
$(function(){
//sourceId levelId industryId
//能不能获得到服务器给我传递的数据
/* var levelStr = "${baseDictLevel.dictId}";
var sourceStr = "${baseDictSource.dictId}";
var industryStr = "${baseDictIndustry.dictId}";
*/
//下拉框已经有字典表的所有对应的数据
//只需要给对应下拉框的option添加一个选中属性即可
/* $("#sourceId option[value='${tempCustomer.baseDictSource.dictId}']").prop("selected" , true);
$("#levelId option[value='${tempCustomer.baseDictLevel.dictId}']").prop("selected" , true);
$("#industryId option[value='${tempCustomer.baseDictIndustry.dictId}']").prop("selected" , true);
*/
$("#sourceId option[value='${tempCustomer.baseDictSource.dictId}']").prop("selected",true);
$("#levelId option[value='${tempCustomer.baseDictLevel.dictId}']").prop("selected",true);
$("#industryId option[value='${tempCustomer.baseDictIndustry.dictId}']").prop("selected",true);
})
13.客户列表-条件查询
@Action("customer_findByCondition")
public String findByCondition() {
//调用service查询字典表数据->级别,所属行业,来源
baseDictLevelList =baseDistService.findBaseDictByTypeCode("006");
baseDictIndstryList =baseDistService.findBaseDictByTypeCode("001");
baseDictSourceList =baseDistService.findBaseDictByTypeCode("009");
//考虑到SQL部分,hibernate提供了离线对象DetachedCriteria,对条件进行了封装。
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
//判断4个条件是否为空[后面先判断对象不为空,再判断属性不为空]
if(!StringUtils.isBlank(customer.getCustName())) {
detachedCriteria.add(Restrictions.like(customer.getCustName(), "%"+customer.getCustName()+"%"));
}
if(customer.getBaseDictLevel()!=null && !StringUtils.isBlank(customer.getBaseDictLevel().getDictId())) {
detachedCriteria.add(Restrictions.eq("baseDictLevel.dictId", customer.getBaseDictLevel().getDictId()));
}
if(customer.getBaseDictSource()!=null && !StringUtils.isBlank(customer.getBaseDictSource().getDictId())) {
detachedCriteria.add(Restrictions.eq("baseDictSource.dictId", customer.getBaseDictSource().getDictId()));
}
if(customer.getBaseDictIndustry()!=null && !StringUtils.isBlank(customer.getBaseDictIndustry().getDictId())) {
detachedCriteria.add(Restrictions.eq("baseDictIndustry.dictId", customer.getBaseDictIndustry().getDictId()));
}
customerList = customerService.findByCondition(detachedCriteria);
return "list";
}
//daoImpl实现类里面,查询数据库的语句 //会自动转换在线对象,进行查询
return (List<Customer>) HibernateTemplate.findByCriteria(detachedCriteria);
一,用户的登录:
1.保证数据能够提交的到服务器
a)表单form的action属性,表单的method属性=post
b)input标签中的name属性
2.服务器的action
- 模型驱动接受数据
- service调用dao查询user
- 响应数据:1)user有值,放在session中 2)user没值跳转到login.jsp显示错误信息。
二,用户退出
友好请求,弹出对话框(是否退出),如果退出--》清空session中的信息。
<A href="javascript:void(0)" onclick="loginout()" target=_top><FONT color=red>安全退出</FONT></A>
javascript:void(0)可以阻止用户运行,让href属性失效。
三,修改密码:
3个原则:密码不为空,两次输入密码一致,用户名不能修改。【获取,处理,相应数据。】
四,字典表中的数据就是公有数据。静态加入字典数据,包含的字段有【id ,name,所属字段类型level,source,industry,类别码】前两个是 多张表公有的数据。
五、客户的增加和删除,在action中添加方法,调用service方法,就可以。
六、客户的修改数据之后的保存:
1.保证表单的action属性,保证组件的name属性(不用动,因为从add页面修改的)
2.手动添加一个隐藏域,目的:服务器根据ID获得指定的数据修改。
3.返回时,在查询一遍,获得所有即可。
七、条件查询-客户列表
1.准备基本数据(下拉框中数据)
2.判断并拼接离线对象的条件
3.查询数据库,返回的结果是customerList,返回list页面
【条件查询时的数据返显】与修改的不同:list.jsp这里的数据封装到模型驱动,edit.jsp的数据存储在临时对象中
action的模型驱动存储数据,响应模型驱动中的数据。