s2sh整合中精确查询功能实现
precision searches.jsp的代码如下<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'list.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript"src="${pageContext.request.contextPath}/js/jquery-1.6.js"></script><script type="text/javascript">/**加载整个文档*/$(document).ready(function() {});function findByName() {//获取id=”uname”文本框中的name的值var name = $("#uname").val();//判断文本框中是否有值 若为空 则弹出红色提示内容:用户名不能为空;以及该文本框变为红色。若不为空则执行submit提交查询if (name == "") {$("#uname").css("border-color", "red");$("#msg").html("<font color='red'>用户名不能为空</font>");} else {$("#suser").submit();}}//文本框中获取焦点时 不显示任何提示信息function styleChanage() {$("#uname").css("border-color", "");$("#msg").html("");}</script></head><body><h1>精确查询操作</h1><h3>唯一性:在用户注册过程中 强调的用户注册的用户名必须唯一</h3><div><formaction="${pageContext.request.contextPath}/csdn/user_findByName"id="suser" method="post"><input type="text" name="user.name" id="uname"onfocus="styleChanage()" /><span id="msg"></span><input type="button" value="精确查询" onclick="findByName()"/></form></div><table><thead><tr><th>序号</th><th>用户名</th><th>密码</th><th>注册时间</th><th>操作</th></tr></thead><!—在tbody中将查询结果显示出来--><tbody><tr><td><s:property value="#request.user.id" /></td><td><s:property value="#request.user.name" /></td><td><s:property value="#request.user.pass" /></td><td><s:date name="#request.user.rdate" format="yyyy-MM-dd"></s:date><br></td><td><s:url id="edit" action="user_edit" namespace="/csdn"><s:param name="user.id"><s:property value="#request.user.id" /></s:param></s:url><s:url id="del" action="user_delete" namespace="/csdn"><s:param name="user.id"><s:property value="#request.user.id" /></s:param></s:url><s:a href="%{edit}">修改</s:a>|<s:a href="%{del}">删除</s:a></td></tr></tbody></table></body></html>
?
UserAction.javapublic class UserAction extends ActionSupport {/** * */private static final long serialVersionUID = 1L;private UserServiceImpl userServiceImpl;public UserServiceImpl getUserServiceImpl() {return userServiceImpl;}/* 依赖注入方式 */public void setUserServiceImpl(UserServiceImpl userServiceImpl) {this.userServiceImpl = userServiceImpl;}private List<User> users;private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public List<User> getUsers() {return users;}public void setUsers(List<User> users) {this.users = users;}/* 精确查询 */public String findByName() {user = userServiceImpl.findByName(user.getName());return "findByName";}}
struts-user.xml文件<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><package name="dxl" extends="json-default" namespace="/csdn"><global-results><result name="input">/index.jsp</result></global-results><action name="user_*" method="{1}"><result name="findByName">/WEB-INF/user/precision searches.jsp</result></action></package></struts>
UserServiceImpl.javapublic class UserServiceImpl implements UserService {private UserDaoImpl userDaoImpl; /*依赖注入*/public void setUserDaoImpl(UserDaoImpl userDaoImpl) {this.userDaoImpl = userDaoImpl;}public User findByName(String name) {return userDaoImpl.findByName(name);}}UserDaoImpl.javapublic class UserDaoImpl extends HibernateDaoSupport implements UserDao{/* 根据用户名查询用户信息 */public User findByName(String name) {List list = getHibernateTemplate().find("from User where name=? ", name);if (list.size() > 0) {User user = (User) list.get(0);return user;}return null;}}
?