Using Struts2 spring hibernate framework development project --------- verify that the user name is available

2011-05-18  来源:本站原创  分类:Web  人气:199 

Using Struts2 spring hibernate framework development project --------- verify that the user name is available

Framework with three major development projects, when we registered users, it often requires the database user name is unique, so we need the user name when registering to verify operation, the operation is how to achieve it? The following example details this content.

Case:

Insert.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                        + request.getServerName() + ":" + request.getServerPort()
                        + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
        <head>
                <base href="<%=basePath%>">

                <title>My JSP 'inseret.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">
 function checkUserName(){
 var name=$("#uname").val();
  $.ajax({
 type:"post",
 url:"csdn/user_checkUserName",
 data:"user.name="+name,
 success:function(data){
      if(data.user==null){
           $("#checkname").html("<font color='red'> User name is available </font>");
      }else{
           $("#checkname").html("<font color='red'> User name not available </font>");
      }
 },
 error:function(jsonmsg){
 },
  dataType:"json"
 });
 }
</script>
        </head>

        <body>
                <h2>
                         User login
                </h2>
                <s:form action="user_insert.action" namespace="/csdn">
                        <s:textfield label=" User name " name="user.name" ></s:textfield>
                        <span></span>
                        <input type="button" value=" Verify that the user name "/>
                        <s:textfield label=" Password " name="user.pass"></s:textfield>
                        <s:submit value=" Add "></s:submit>
                </s:form>

        </body>
</html>

Analysis: In order to verify the user name is available, I added a button to verify the user name is available. The button will trigger the onclick = "checkUserName ()" event, using the ajax Jquery request processing, the text box from the page you want to register for a user name, click the Verify button to verify the user name, use ajax to post the request to the user were reached in the implementation of action checkUserName () method, the following will be based on the user name to the underlying implementation class in the search process.

Action.Java
public class UserAction extends ActionSupport {

        /**
         *
         */
        private static final long serialVersionUID = 1L;
        private UserServiceImpl userServiceImpl;
        public UserServiceImpl getUserServiceImpl() {
                return userServiceImpl;
        }

        /*  Dependency injection  */
        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;
        }
/* Check user name availability */
        public String checkUserName(){
                user=userServiceImpl.findByName(user.getName());
                return "checkUserName";
        }
}
UserServiceImpl.java
public class UserServiceImpl implements UserService {

        private UserDaoImpl userDaoImpl;
   /* Dependency injection */
        public void setUserDaoImpl(UserDaoImpl userDaoImpl) {
                this.userDaoImpl = userDaoImpl;
        }
        public User findByName(String name) {

                return userDaoImpl.findByName(name);

        }
}
UserDaoImpl.java
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
        /*  Based on user query user information  */
        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;
        }
}
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}">
                        <!--  Returned is a user object  user Object (id,name,pass) -->
                        <result name="checkUserName" type="json">
                                <!-- param Parameter configuration ,includeProperties -->
                                <param name="includeProperties">
                                        user\.id,user\.name
                 </param>
                        </result>
</action>
</struts>

Analysis: This file uses a wildcard, return a json type, includeProperties parameters: the output results need to include the property value, where the regular expression and attribute names match, you can use, "" regular expression split over fill type.

相关文章
  • Using Struts2 spring hibernate framework development project --------- verify that the user name is available 2011-05-18

    Using Struts2 spring hibernate framework development project --------- verify that the user name is available Framework with three major development projects, when we registered users, it often requires the database user name is unique, so we need th

  • Struts2 + Spring + Hibernate + DWR to achieve urban linkage 2010-06-25

    In the "DWR framework to use" article describes a basic overview of DWR and basic configuration. This took some time to achieve a Struts2 + Spring + Hibernate + DWR urban linkage to achieve a small item, I feel very useful. According to MVC hier

  • Manage projects using Maven ----- create Struts2, Spring, Hibernate project (Reprinted order) 2011-05-24

    Maven should have heard it! A build tool! Compared with Ant, it is more coarse-grained! No need to write build.xml file, to operate the project compile, run, packing! As long as you create a Maven project, the compiler, just mvn compile! Maven will a

  • Manage projects using Maven ----- create Struts2, Spring, Hibernate project 2011-05-24

    Maven should have heard it! A build tool! Compared with Ant, it is more coarse-grained! No need to write build.xml file, to operate the project compile, run, packing! As long as you create a Maven project, the compiler, just mvn compile! Maven will a

  • Struts2 + Spring + Hibernate build the whole solution! 2010-04-02

    Struts2 + Spring + Hibernate is the latest popular J2EE framework. This is my set up the framework of lessons learned, many people set up the framework of the Association met Large and small problems, nothing online is also effective programs or intr

  • 3.Struts2 + Spring + Hibernate Integration (continued Struts2 + Spring integration) 2009-04-27

    As already described Struts2 + Spring integration, where we then integrate Hibernate Hibernate Integration 1) Import Hibernate3.1 the jar package: antlr-2.7.6.jar, commons-collections-3.1.jar, dom4j-1.6.1.jar, javassist-3.4.GA.jar, jta-1.1.jar, hiber

  • Struts2 spring hibernate integration example 2009-10-26

    Private http://www.blogjava.net/fundei/archive/2009/05/14/270668.html Integrate struts, spring, hibernate, for the beginner is the biggest trouble on its cumbersome xml configuration file. All of which are now based on the configuration of notes prov

  • Struts2 + Spring + Hibernate to build the whole solution! 2008-07-14

    Struts2 + Spring + Hibernate to build the whole solution! Struts2 + Spring + Hibernate is the latest popular J2EE framework. This article is my summary of the experiences to build this framework, there are a lot of people will always encounter this f

  • Integrated struts2 spring hibernate, using annotations 2011-05-08

    Integrated struts2 spring hibernate, using annotations 2009-04-24 10:43 Integrate struts, spring, hibernate, the biggest problem for beginners on its cumbersome xml configuration file. Now all of which are annotation-based configuration provides good

  • S2SH (Struts2 + Spring + Hibernate) integration profile 2009-07-26

    S2SH (Struts2 + Spring + Hibernate) integration profile 1.web.xml file configuration is as follows: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmln

  • struts2 + spring + hibernate integration configuration 2009-08-13

    struts2 + spring + hibernate configuration: 1, web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-

  • Struts2.Spring.Hibernate podcast PPT Chi Chuan 2010-11-02

    Struts2, Spring, Hibernate Chi Chuan Podcasts

  • Struts2 + Spring + Hibernate 通用 Service 和 DAO 2014-08-16

    我在 Struts2 + Spring + Hibernate 项目开发中总结出了一个Service 和 DAO ,可以用于处理任何的pojo(bean).使用这两个Service 和 DAO 可以极大地提高开发的效率,不必再分别针对不同的pojo编写对应的Service 和 DAO.内容如下: DAO: 接口:BaseDao.java package dao; import java.util.List; /** * @author 雷霄骅 * 对Object的DAO操作 * 提供了通用的一些

  • Struts2,Spring,Hibernate框架的优缺点 2014-11-21

    Struts2,Spring,Hibernate框架的优缺点 分类: SSH框架整合 2010-05-26 21:27 5432人阅读 评论(0) 收藏 举报 strutsspringhibernate框架daomvc 开源是3个框架共有的优点 Struts2框架(MVC框架)的优点如下: 1) 实现了MVC模式,层次结构清晰,使程序员只需关注业务逻辑的实现: 2) 丰富的标签库,大大提高了开发的效率: 3) Struts2提供丰富的拦截器实现 3) 通过配置文件,就可以掌握整个系统各个部分之间

  • Dao spring + hibernate framework of several ways to access the database 2010-06-25

    In the spring + hibernate in the structure, there are several ways to access the database by spring dependency injection to distinguish between three kinds, and in this look at again before spring dependency injection, spring the two main core is IOC

  • Flash Builder 4 + BlazeDs + Spring + Hibernate + Cairngorm development framework 2010-12-14

    A year ago, the project needs, contact the Flex3 development, has since branched out, fell in love with Flex this technology. Has long been the silent majority of users also help, recently spent two months, order a bit, the Flex development framework

  • Struts2 + Spring + Hibernate + Json integration and development issues 2010-05-02

    Have a period of time, no update blog, because recent work is a busy time now to write about some of the problems recently encountered it. Extjs to do with a recent OA (http://www.liuhx.cn) Why use extjs to develop it? Because extjs give the user a n

  • Under the MyEclipse integrated struts2.spring.hibernate 2009-05-31

    1, MyEclipse set up under the new Web Porject 2, Copy Struts2 the lib file to the project under WEB-INF/lib non-*- plugin-2.0.6.jar package with all the struts2-spring-plugin-2.0.6.jar, the smallest package in struts -2.0.6 \ apps \ struts2-blank-2.0

  • eclipse下搭建SSH整合环境(Struts2+Spring+Hibernate+maven) 2014-05-08

    1,创建一个maven工程,在选择Archetype时选择webapp: 2,下一步配置maven的pom.xml文件,获取依赖的jar包: <!-- struts2核心包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.1.2</version> &

  • Struts2 Spring Hibernate's easy to integrate 2009-02-27

    1. Add Spring 2.0 in Libraries Choose the following four jar, and configure the / WEB-INF/lib under Spring2.0 AOP Libraries Spring2.0 Core Libraries Spring2.0 Persistence Core Libraries Spring2.0 WEb Libraries At the same time, the applicationContext