在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例J2ME平台开发 → 手机网店

手机网店

J2ME平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:1.57M
  • 下载次数:24
  • 浏览次数:285
  • 发布时间:2015-12-03
  • 实例类别:J2ME平台开发
  • 发 布 人:泽行天下
  • 文件格式:.doc
  • 所需积分:5
 相关标签: 手机

实例介绍

【实例简介】
【实例截图】
【核心代码】
 

IUserDAO接口

public interface IUserDAO {

    public void saveUser(User user);

    public User validateUser(String username,String password);

    public boolean exitUser(String username);

    public Admin validateAdmin(String username, String password);

}

UserDAO

public class UserDAO extends BaseDAO implements IUserDAO{

    public void saveUser(User user){

        Session session=getSession();

        Transaction tx=session.beginTransaction();

        session.save(user);

        tx.commit();

        session.close();

    }

    public User validateUser(String username,String password){     

        String sql="from User u where u.username=? and u.password=?";

        Session session=getSession();

        Query query=session.createQuery(sql);

        query.setParameter(0,username);

        query.setParameter(1,password);

        List users=query.list();

        if(users.size()!=0)

        {

            User user=(User)users.get(0);

            return user;

        }

        session.close();

        return null;

    }

    public boolean exitUser(String username){

        Session session=getSession();

        String hql="from User u where u.username=? ";

        Query query=session.createQuery(hql);

        query.setParameter(0,username);

        List users=query.list();

        if(users.size()!=0){

            User user=(User)users.get(0);

            return true;

        }

        session.close();

        return false;

    }

    @Override

    public Admin validateAdmin(String username, String password) {

        String sql="from Admin u where u.username=? and u.password=?";

        Session session=getSession();

        Query query=session.createQuery(sql);

        query.setParameter(0,username);

        query.setParameter(1,password);

        List admins=query.list();

        if(admins.size()!=0)

        {

            Admin admin=(Admin)admins.get(0);

            return admin;

        }

        session.close();

        return null;

    }

}

2.2.2 开发Service

IUserService

public interface IUserService {

    public void saveUser(User user);

    public User validateUser(String username,String password);

    public Admin validateAdmin(String username,String password);

    public boolean exitUser(String username);

}

UserService

public class UserService implements IUserService{

    private IUserDAO userDAO;

    public void saveUser(User user){

        this.userDAO.saveUser(user);

    }

    public User validateUser(String username,String password){

        return userDAO.validateUser(username, password);

    }

    public Admin validateAdmin(String username,String password){

        return userDAO.validateAdmin(username, password);

    }

    public boolean exitUser(String username){

        return userDAO.exitUser(username);

    }

    public IUserDAO getUserDAO(){

        return userDAO;

    }

    public void setUserDAO(IUserDAO userDAO){

        this.userDAO=userDAO;

    }

}

2.2.3 开发Action模块

UserAction

public class UserAction extends ActionSupport{

    private User user;

    protected IUserService userService;

    private int type;

    public int getType() {

        return type;

    }

    public void setType(int type) {

        this.type = type;

    }

    //用户注册,调用service层的saveUser()方法

    public String register() throws Exception{

        userService.saveUser(user);

        return SUCCESS;

    }

    //用户登录,调用service层的validateUser()方法

    public String execute() throws Exception{

        User u=null;

        Admin a = null;

        if(getType()==0)

           u=userService.validateUser(user.getUsername(),user.getPassword());

        else  a=userService.validateAdmin(user.getUsername(),user.getPassword());

        if(u!=null || a!=null)

        {

            Map session=ActionContext.getContext().getSession();

            //保存此次会话的u(用户账号)信息

            if(getType()==0){

            session.put("user", u);

            session.put("username",u.getUsername());

            session.put("userid",u.getUserid());

            session.put("type",0);

            }

            else

            {  

                session.put("username",a.getUsername());

                session.put("type",1);

            }

            return SUCCESS;

        }

        return ERROR;

    }

    //用户注销,去除会话中的用户账号信息即可,无须调用service

    public String logout() throws Exception{

        Map session=ActionContext.getContext().getSession();

        session.remove("user");

        session.remove("username");

        session.remove("userid");

        session.remove("type");

        //session.remove("cart");

        return SUCCESS;

    }

    public User getUser(){

        return this.user;

    }

    public void setUser(User user){

        this.user=user;

    }

    public IUserService getUserService(){

        return this.userService;

    }

    public void setUserService(IUserService userService){

        this.userService=userService;

    }

}

2.2.3 开发Action模块

public class UserAction extends ActionSupport{

    private User user;

    protected IUserService userService;

    private int type;

    public int getType() {

        return type;

    }

    public void setType(int type) {

        this.type = type;

    }

    public String register() throws Exception{

        userService.saveUser(user);

        return SUCCESS;

    }

        public String execute() throws Exception{

        User u=null;

        Admin a = null;

        if(getType()==0)

       u=userService.validateUser(user.getUsername(),user.getPassword());

        else     a=userService.validateAdmin(user.getUsername(),user.getPassword());

        if(u!=null || a!=null)

        {

            Map session=ActionContext.getContext().getSession();

            if(getType()==0){

            session.put("user", u);

            session.put("username",u.getUsername());

            session.put("userid",u.getUserid());

            session.put("type",0);

            }

            else

            {  

                session.put("username",a.getUsername());

                session.put("type",1);

            }

            return SUCCESS;

        }

        return ERROR;

    }

        public String logout() throws Exception{

        Map session=ActionContext.getContext().getSession();

        session.remove("user");

        session.remove("username");

        session.remove("userid");

        session.remove("type");

        //session.remove("cart");

        return SUCCESS;

    }

}

2.2.4 配置Action

<action name="register" class="userAction" method="register" >

            <result name="success">/register_success.jsp</result>

        </action>

        <action name="login" class="userAction">

            <result name="success">/login_success.jsp</result>

            <result name="error">/login.jsp</result>

        </action>

        <action name="logout" class="userAction" method="logout">

            <result name="success">/index.jsp</result>

        </action>

 

2.3 浏览及分类搜索模块

2.3.1 开发DAO

IPhoneDAO

public interface IPhoneDAO {

    public List getPhonebyCatalogid(Integer catalogid);

    public List getPhonebyCatalogidPaging(Integer catalogid,int currentPage,int pageSize);

    public int getTotalbyCatalog(Integer catalogid);

    public List getRequiredPhonebyHql(String hql);

    public Phone getPhonebyId(Integer phoneid);

    public void save(Phone phone);

    public int executehql(String hql);

}

PhoneDAO

public class PhoneDAO extends BaseDAO implements IPhoneDAO{

    public List getPhonebyCatalogid(Integer catalogid){

        Session session=getSession();

        Query query=null;

        if(catalogid==0)

        {

          query=session.createQuery("from Phone b");

        }

        else

        {

        query=session.createQuery("from Phone b where b.catalog.catalogid=?");

        query.setParameter(0, catalogid);

        }

        List phones=query.list();

        session.close();

        return phones;

    }

    public List getPhonebyCatalogidPaging(Integer catalogid,int currentPage,int pageSize){

        Session session=getSession();

        Query query=null;

        if(catalogid==0)

        {

          query=session.createQuery("from Phone b");

        }

        else

        {

        query=session.createQuery("from Phone b where b.catalog.catalogid=?");

        query.setParameter(0, catalogid);

        }

        //确定起始游标的位置

        int startRow=(currentPage-1)*pageSize;

        query.setFirstResult(startRow);

        query.setMaxResults(pageSize);

        List phones=query.list();

        session.close();

        return phones;

    }

    public int getTotalbyCatalog(Integer catalogid){

       

        List phones=getPhonebyCatalogid(catalogid);

        int totalSize=phones.size();

        return totalSize;

    }

    public List getRequiredPhonebyHql(String hql) {

        Session session=getSession();

        Query query=session.createQuery(hql);

        List phones=query.list();

        session.close();

        return phones;

    }

    //根据图书号得到图书

    public Phone getPhonebyId(Integer phoneid){

        Session session=getSession();

        Phone phone=(Phone)session.get(Phone.class,phoneid);

        session.close();

        return phone;

    }

    @Override

    public void save(Phone phone) {

        Session session = getSession();

        Transaction tx = session.beginTransaction();

        session.save(phone);

        tx.commit();

        session.close();

    }

    @Override

    public int executehql(String hql) {

        Session session = getSession();

        Transaction tx = session.beginTransaction();

        Query queryupdate=session.createQuery(hql);

        int ret = queryupdate.executeUpdate();

        tx.commit();

        session.close();

        return ret;

    }

}

2.3.2 开发Service

IPhoneService

public interface IPhoneService {

    public List getPhonebyCatalogid(Integer catalogid);

    public List getPhonebyCatalogidPaging(Integer catalogid,int currentPage,int pageSize);

    public int getTotalbyCatalog(Integer catalogid);

    public List getRequiredPhonebyHql(String hql);

    public Phone getPhonebyId(Integer phoneid);

    public void save(Phone phone);

    public int executehql(String hql);

}

PhoneService

public class PhoneService implements IPhoneService{

    private IPhoneDAO phoneDAO;

    public List getPhonebyCatalogid(Integer catalogid){

        return phoneDAO.getPhonebyCatalogid(catalogid);

    }

    public List getPhonebyCatalogidPaging(Integer catalogid,int currentPage,int pageSize){

        return phoneDAO.getPhonebyCatalogidPaging(catalogid, currentPage, pageSize);

    }

    public int getTotalbyCatalog(Integer catalogid){

        return phoneDAO.getTotalbyCatalog(catalogid);

    }

    public List getRequiredPhonebyHql(String hql) {

        return phoneDAO.getRequiredPhonebyHql(hql);

    }

    public IPhoneDAO getPhoneDAO() {

        return phoneDAO;

    }

    public void setPhoneDAO(IPhoneDAO phoneDAO) {

        this.phoneDAO=phoneDAO;

    }

    public Phone getPhonebyId(Integer phoneid){

        return phoneDAO.getPhonebyId(phoneid);

    }

    @Override

    public void save(Phone phone) {

        // TODO Auto-generated method stub

        phoneDAO.save(phone);

    }

    @Override

    public int executehql(String hql) {

        return phoneDAO.executehql(hql);

    }

}

2.3.3 开发Action组件

PhoneAction

public class PhoneAction extends ActionSupport{

    protected ICatalogService catalogService;

    protected IPhoneService phoneService;

    private Integer catalogid;

    private String picture;

    private Integer price;

    private Integer phoneid;

   

    private Integer currentPage=1;

    private String phonename;

    private String search;

    private int lowprice;

    private int highprice;

    private int userid;

   

    public String browseCatalog() throws Exception{

        List catalogs=catalogService.getAllCatalogs();

        Map request=(Map)ActionContext.getContext().get("request");

        request.put("catalogs", catalogs);

        return SUCCESS;

    }

    public String browsePhone() throws Exception{

        List phones=phoneService.getPhonebyCatalogid(catalogid);

        Map request=(Map)ActionContext.getContext().get("request");

        request.put("phones", phones);

        return SUCCESS;

    }

    public String browsePhonePaging() throws Exception{

        int totalSize=phoneService.getTotalbyCatalog(catalogid);

        Pager pager=new Pager(currentPage,totalSize);

        List phones=phoneService.getPhonebyCatalogidPaging(catalogid,currentPage, pager.getPageSize());

        Map request=(Map)ActionContext.getContext().get("request");

        request.put("phones", phones);

        request.put("pager",pager);

        //购物车要返回时,需要记住返回的地址

        Map session=ActionContext.getContext().getSession();

        request.put("catalogid",catalogid);

        return SUCCESS;

    }

    public String searchPhone() throws Exception {

        StringBuffer hql=new StringBuffer("from Phone b ");

        if(phonename!=null&&phonename.length()!=0)

            hql.append("where b.phonename like '%" phonename "%'");

        List phones=phoneService.getRequiredPhonebyHql(hql.toString());

        Map request=(Map)ActionContext.getContext().get("request");

        int totalSize=phones.size();

        request.put("phones",phones);

        return SUCCESS;

    }

    public String searchPhoneByPrice() throws Exception {

        StringBuffer hql=new StringBuffer("from Phone b ");

        hql.append("where b.price>=" getLowprice() "and b.price<=" getHighprice());

        List phones=phoneService.getRequiredPhonebyHql(hql.toString());

        Map request=(Map)ActionContext.getContext().get("request");

        int totalSize=phones.size();

        request.put("phones",phones);

        return SUCCESS;

    }

   

    public String modifyphone_handle() throws Exception {

   

        String hql = "update from Phone b set ";

        hql ="b.phonename='" getPhonename() "' ";

        hql =",b.price=" getPrice() " ";

        hql ="where b.phoneid=" getPhoneid();

        phoneService.executehql(hql);

        return SUCCESS;

    }

    public String deletephone_handle() throws Exception {

       

        String hql = "delete from Phone b ";

        hql ="where b.phoneid=" getPhoneid();

        phoneService.executehql(hql);

        return SUCCESS;

    }

    public String addphone_handle() throws Exception {

       

        if(getPicture()==null || getPhonename()==null || getPrice()==null)return ERROR;

        String picture="";

        switch(getCatalogid())

        {

        case 1: picture="sanxing/";break;

        case 2: picture="huawei/";break;

        case 3: picture="xiaomi/";break;

        case 4: picture="apple/";break;

        case 5: picture="lenovo/";break;

        }

        picture =getPicture();

        Phone phone=new Phone();

        Catalog catalog = new Catalog();

        catalog.setCatalogid(getCatalogid());

        phone.setPhonename(getPhonename());

        phone.setPrice(getPrice());

        phone.setPicture(picture);

        phone.setCatalog(catalog);

        phoneService.save(phone);

        return SUCCESS;

    }

2.3.4 配置Action

    <action name="browseCatalog" class="phoneAction" method="browseCatalog">

            <result name="success">/menu.jsp</result>

        </action>

        <action name="browsePhone" class="phoneAction" method="browsePhone">

            <result name="success">/browsePhone.jsp</result>

        </action>

   

        <action name="browsePhonePaging" class="phoneAction" method="browsePhonePaging">

            <result name="success">/browsePhonePaging.jsp</result>

        </action>

        <action name="searchPhone" class="phoneAction" method="searchPhone">

            <result name="success">/searchPhone_result.jsp</result>

        </action>

        <action name="searchPhoneByPrice" class="phoneAction" method="searchPhoneByPrice">

            <result name="success">/searchPhone_result.jsp</result>

        </action>

2.4 购物车及订单模块

2.4.1 开发DAO

IOrderDAO

public interface IOrderDAO {

    public Orders saveOrder(Orders order);

    public int modifyOrderitem(String hql);

    public List getRequiredbyHql(String hql);

    public List getRequiredbyUserid(int userid);

    public List getRequiredbyOrderid(int orderid);

}

OrderDAO

public class OrderDAO extends BaseDAO implements IOrderDAO{

   

    public Orders saveOrder(Orders order) {

        Session session = getSession();

        Transaction tx = session.beginTransaction();

        session.save(order);

        tx.commit();

        session.close();

        return order;

    }

    public int modifyOrderitem(String hql) {

        Session session = getSession();

        Transaction tx = session.beginTransaction();

        Query queryupdate=session.createQuery(hql);

        int ret = queryupdate.executeUpdate();

        tx.commit();

        session.close();

        return ret;

    }

    public List getRequiredbyHql(String hql) {

        Session session=getSession();

        Query query=session.createQuery(hql);

        List orders=query.list();

        session.close();

        return orders;

    }

    @Override

    public List getRequiredbyUserid(int userid) {

        String hql = "from Orders o where o.user=" userid;

        return getRequiredbyHql(hql);

    }

    public List getRequiredbyOrderid(int orderid) {

        String hql = "from Orderitem o,Phone p where o.orders=" orderid "and o.phone=p.phoneid";

        return getRequiredbyHql(hql);

    }

}

2.4.2 开发Service

IOrderService

public interface IOrderService {

    public Orders saveOrder(Orders order);

    public int modifyOrderitem(String hql);

    public List queryorders(int userid);

    public List queryorderitems(int orderid);

    public List queryorderbyhql(String hql);

}

IOrderService

public class OrderService implements IOrderService{

    private IOrderDAO orderDAO;

    public void setOrderDAO(IOrderDAO orderDAO) {

        this.orderDAO=orderDAO;

    }

    public Orders saveOrder(Orders order) {

        return orderDAO.saveOrder(order);

    }

    public List queryorders(int userid)

    {

        return orderDAO.getRequiredbyUserid(userid);

    }

    public List queryorderitems(int orderid)

    {

        return orderDAO.getRequiredbyOrderid(orderid);

    }

    @Override

    public List queryorderbyhql(String hql) {

        // TODO Auto-generated method stub

        return orderDAO.getRequiredbyHql(hql);

    }

    @Override

    public int modifyOrderitem(String hql) {

        // TODO Auto-generated method stub

        return orderDAO.modifyOrderitem(hql);

    }

}

2.4.3 开发Action

ShoppingAction

public class ShoppingAction extends ActionSupport{

    //添加到购物车

    public String addToCart() throws Exception{

        Phone phone=phoneService.getPhonebyId(phoneid);

        Orderitem orderitem=new Orderitem();

        orderitem.setPhone(phone);

        orderitem.setQuantity(quantity);

        Map session=ActionContext.getContext().getSession();

        Cart cart=(Cart)session.get("cart");

        if(cart==null){

            cart=new Cart();

        }

        cart.addPhone(phoneid, orderitem);

        session.put("cart",cart);

        return SUCCESS;

    }

   

    //更新购物车

    public String updateCart() throws Exception{

        Map session=ActionContext.getContext().getSession();

        Cart cart=(Cart)session.get("cart");

        cart.updateCart(phoneid, this.getQuantity());

        session.put("cart", cart);

        return SUCCESS;

    }

    //结账下订单

    public String checkout() throws Exception{

        Map session=ActionContext.getContext().getSession();

        User user=(User)session.get("user");

        Cart cart=(Cart)session.get("cart");

        if(user==null || cart ==null)

            return ActionSupport.ERROR;

        Orders order=new Orders();

        order.setOrderdate(new Date());

        order.setUser(user);

        for(Iterator it=cart.getItems().values().iterator();it.hasNext();){

            Orderitem orderitem=(Orderitem)it.next();

            orderitem.setOrders(order);

            orderitem.setStatus(0);

            order.getOrderitems().add(orderitem);

        }

        orderService.saveOrder(order);

        Map request=(Map)ActionContext.getContext().get("request");

        request.put("order",order);

        return SUCCESS;

    }

   

    public String queryorders() throws Exception {

        List orders=orderService.queryorders(getUserid());

        Map request=(Map)ActionContext.getContext().get("request");

        request.put("orders",orders);

        return SUCCESS;

    }

   

    public String queryorderitems() throws Exception {

        List orderitems=orderService.queryorderitems(getOrderid());

        Map request=(Map)ActionContext.getContext().get("request");

        request.put("orderitems",orderitems);

        request.put("orderid",getOrderid());

        Map session=ActionContext.getContext().getSession();

        session.put("orderid",getOrderid());

        return SUCCESS;

    }

   

    public String modifystatus() throws Exception {

        String hql = "update Orderitem o set o.status=" getStatus() " where o.orderitemid=" getOrderitemid();

        int ret =orderService.modifyOrderitem(hql);

        if(ret>0)

        return SUCCESS;

        else return ERROR;

    }

   

    public String comment() throws Exception {

       

        String hql = "update Orderitem o set o.comment='" getComment() "' where o.orderitemid=" getOrderitemid();

        int ret =orderService.modifyOrderitem(hql);

        hql = "update Orderitem o set o.status=" 2 " where o.orderitemid=" getOrderitemid();

        ret =orderService.modifyOrderitem(hql);

        if(ret>0)

        return SUCCESS;

        else return ERROR;

    }

   

2.4.4 配置Action

<action name="addToCart" class="shoppingAction" method="addToCart">

            <result name="success">/addToCart_success.jsp</result>

        </action>

        <action name="queryorders" class="shoppingAction" method="queryorders">

            <result name="success">/showorders.jsp</result>

        </action>

        <action name="queryorderitems" class="shoppingAction" method="queryorderitems">

            <result name="success">/showorderitems.jsp</result>

        </action>

        <action name="updateCart" class="shoppingAction" method="updateCart">

            <result name="success">/showCart.jsp</result>

        </action>

        <action name="checkout" class="shoppingAction" method="checkout">

            <result name="success">/checkout_success.jsp</result>

            <result name="error">/login.jsp</result>

        </action>

        <action name="comment" class="shoppingAction" method="comment">

            <result name="success">/showorderdetail.jsp</result>

        </action>

2.4.5 购物车类

public class Cart {

    protected Map<Integer,Orderitem> items;//属性item

    //构造函数

    public Cart(){

        if(items==null)

            items=new HashMap<Integer,Orderitem>();

    }

    //添加图书到购物车

    public void addPhone(Integer phoneid,Orderitem orderitem){

        //是否存在,如果存在,更改数量

        //如果不存在的话,添加入集合

        if(items.containsKey("phoneid")){

            Orderitem _orderitem=items.get(phoneid);

            orderitem.setQuantity(_orderitem.getQuantity() orderitem.getQuantity());

            items.put(phoneid,orderitem);

        }

        else{

            items.put(phoneid,orderitem);

        }

    }

    //更新购物车的购买书籍数量

    public void updateCart(Integer phoneid,int quantity){

        Orderitem orderitem=items.get(phoneid);

        orderitem.setQuantity(quantity);

        items.put(phoneid, orderitem);

    }

    //计算总价格

    public int getTotalPrice(){

        int totalPrice=0;

        for(Iterator it=items.values().iterator();it.hasNext();){

            Orderitem orderitem=(Orderitem)it.next();

            Phone phone=orderitem.getPhone();

            int quantity=orderitem.getQuantity();

            totalPrice =phone.getPrice()*quantity;

        }

        return totalPrice;

    }

   

    public Map<Integer, Orderitem> getItems() {

        return items;

    }

    public void setItems(Map<Integer, Orderitem> items) {

        this.items=items;

    }}

2.5  修改订单状态及评论模块

2.5.1 开发DAO

DAO 包含在了OrderDAO

2.5.2 开发Service

Service 包含在了OrderService

2.5.3 开发Action

public String modifystatus() throws Exception {

        String hql = "update Orderitem o set o.status=" getStatus() " where o.orderitemid=" getOrderitemid();

        int ret =orderService.modifyOrderitem(hql);

        if(ret>0)

        return SUCCESS;

        else return ERROR;

    }

   

    public String comment() throws Exception {

       

        String hql = "update Orderitem o set o.comment='" getComment() "' where o.orderitemid=" getOrderitemid();

        int ret =orderService.modifyOrderitem(hql);

        hql = "update Orderitem o set o.status=" 2 " where o.orderitemid=" getOrderitemid();

        ret =orderService.modifyOrderitem(hql);

        if(ret>0)

        return SUCCESS;

        else return ERROR;

    }

2.5.4 配置Action

<action name="comment" class="shoppingAction" method="comment">

            <result name="success">/showorderdetail.jsp</result>

        </action>

        <action name="modifystatus" class="shoppingAction" method="modifystatus">

            <result name="success">/showorderdetail.jsp</result>

        </action>

2.数据库设计

 数据库表结构


手机品牌分类

分类编号   int  主键  非空

品牌名称 varchar      非空

品牌logo varchar         非空

手机

手机编号   int  主键  非空

手机名称 varchar      非空

价格    int               非空

图片id  varchar       非空

订单项

订单项编号   int  主键  非空

手机编号     int        非空

订单编号     int            非空

数量         int        非空

评论           varchar     非空

状态              int             非空

订单

订单编号   int  主键  非空

用户编号     int        非空

订单日期       Date               非空

普通用户

用户编号   int  主键  非空

姓名     varchar      非空

密码       varchar                非空

年龄     int             

性别    varchar      

管理员用户

用户编号   int  主键  非空

姓名     varchar      非空

密码     varchar               非空    

1........n

1........n

1

.

.

n

1

.

.

n



3.系统公用类与接口

编码过滤器类

public class CharacterEncodingFilter implements Filter {   

        

         protected FilterConfig filterConfig = null;        

         protected String encoding = "";   

        

         public void destroy() {   

             filterConfig = null;   

             encoding = null;   

         }   

        

         public void doFilter(ServletRequest request, ServletResponse response,   

                 FilterChain filterChain) throws IOException, ServletException {   

             if (encoding != null) {  

                 //设置requestresponse的编程格式,注意两个都要设,若没设response 

                 //charset,则在输出页面会显示乱码。 

                 request.setCharacterEncoding(this.encoding);  

                 response.setContentType("text/html;charset=utf-8"); 

             }   

             //继续执行下一个过滤器 

             filterChain.doFilter(request, response);   

         }   

        

         public void init(FilterConfig filterConfig) throws ServletException {   

             this.filterConfig = filterConfig;   

            this.encoding = filterConfig.getInitParameter("encoding");   

             

         }   

     } 

分页控制类

public class Pager {

    private int currentPage;//当前页面

    private int pageSize=6;//每页的记录数,此处赋了一个初始值,每页显示3

    private int totalSize;//总的记录数

    private int totalPage;//总的页数,由总的记录数除以每页的记录数得到:totalSize/pageSize

   

    private boolean hasFirst;//是否有第一页

    private boolean hasPrevious;//是否有上一页

    private boolean hasNext;//是否有下一页

    private boolean hasLast;//是否有最后一页

    //构造函数,传递当前页、总的记录数

    public Pager(int currentPage,int totalSize){

        this.currentPage=currentPage;

        this.totalSize=totalSize;

    }

   

    public int getCurrentPage() {

        return currentPage;

    }

    public void setCurrentPage(int currentPage) {

        this.currentPage=currentPage;

    }

   

    public int getPageSize() {

        return pageSize;

    }

    public void setPageSize(int pageSize) {

        this.pageSize=pageSize;

    }

   

    public int getTotalSize() {

        return totalSize;

    }

    public void setTotalSize(int totalSize) {

        this.totalSize=totalSize;

    }

   

    public int getTotalPage() {

        totalPage=totalSize/pageSize;

        if(totalSize%pageSize!=0)

            totalPage ;

        return totalPage;

    }

    public void setTotalPage(int totalPage) {

        this.totalPage=totalPage;

    }

   

    public boolean isHasFirst() {

        if(currentPage==1){

            return false;

        }

        return true;

    }

    public void setHasFirst(boolean hasFirst) {

        this.hasFirst=hasFirst;

    }

   

    public boolean isHasPrevious() {

        if(isHasFirst())

            return true;

        else

            return false;

    }

    public void setHasPrevious(boolean hasPrevious) {

        this.hasPrevious=hasPrevious;

    }

   

    public boolean isHasNext() {

        if(isHasLast())

            return true;

        else

            return false;

    }

    public void setHasNext(boolean hasNext) {

        this.hasNext=hasNext;

    }

   

    public boolean isHasLast() {

        if(currentPage==getTotalPage())

            return false;

        else

            return true;

    }

    public void setHasLast(boolean hasLast) {

        this.hasLast=hasLast;

    }

}

4.核心配置文件

5.1 web.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<filter>   

   <filter-name>CharacterEncodingFilter</filter-name>   

   <filter-class>org.easyphones.filter.CharacterEncodingFilter</filter-class>     

      <init-param>   

        <param-name>encoding</param-name>   

        <param-value>utf-8</param-value>   

    </init-param>   

  </filter>   

 <filter-mapping>      

    <filter-name>CharacterEncodingFilter</filter-name>   

    <url-pattern>/*</url-pattern>   

  </filter-mapping> 

 

    <filter>

        <filter-name>struts2</filter-name>

        <filter-class>

            org.apache.struts2.dispatcher.FilterDispatcher

        </filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

    <listener>

        <listener-class>

            org.springframework.web.context.ContextLoaderListener

         </listener-class>

    </listener >

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>

            /WEB-INF/classes/applicationContext.xml

         </param-value>

    </context-param>

    <!-- 开始DWR配置 -->

    <servlet>

        <servlet-name>dwr</servlet-name>

        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

        <init-param>

            <param-name>debug</param-name>

            <param-value>true</param-value>

        </init-param>

        <init-param>

            <param-name>crossDomainSessionSecurity</param-name>

            <param-value>false</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>dwr</servlet-name>

        <url-pattern>/dwr/*</url-pattern>

    </servlet-mapping>

    <!-- 结束DWR配置 -->   

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

5.2  Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans

    xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="dataSource"

        class="org.apache.commons.dbcp.BasicDataSource">

        <property name="driverClassName"

            value="com.mysql.jdbc.Driver">

        </property>

        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>

        <property name="username" value="root"></property>

        <property name="password" value="19830925"></property>

    </bean>

    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource">

            <ref bean="dataSource" />

        </property>

        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">

                    org.hibernate.dialect.MySQLDialect

                </prop>

            </props>

        </property>

        <property name="mappingResources">

            <list>

                <value>org/easyphones/phonestore/vo/Phone.hbm.xml</value>

                <value>

                    org/easyphones/phonestore/vo/Catalog.hbm.xml

                </value>

                <value>

                    org/easyphones/phonestore/vo/Orderitem.hbm.xml

                </value>

                <value>org/easyphones/phonestore/vo/Orders.hbm.xml</value>

                <value>org/easyphones/phonestore/vo/User.hbm.xml</value>

            </list>

        </property>

    </bean>

    <bean id="baseDAO" class="org.easyphones.phonestore.dao.BaseDAO">

        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    <bean id="userDAO" class="org.easyphones.phonestore.dao.impl.UserDAO" parent="baseDAO"/>

    <bean id="userService" class="org.easyphones.phonestore.service.impl.UserService">

        <property name="userDAO" ref="userDAO"/>

    </bean>

    <bean id="userAction" class="org.easyphones.phonestore.action.UserAction">

        <property name="userService" ref="userService"/>

    </bean>

    <bean id="catalogDAO" class="org.easyphones.phonestore.dao.impl.CatalogDAO" parent="baseDAO"/>

    <bean id="catalogService" class="org.easyphones.phonestore.service.impl.CatalogService">

        <property name="catalogDAO" ref="catalogDAO"/>

    </bean>

    <bean id="phoneDAO" class="org.easyphones.phonestore.dao.impl.PhoneDAO" parent="baseDAO"/>

    <bean id="phoneService" class="org.easyphones.phonestore.service.impl.PhoneService">

        <property name="phoneDAO" ref="phoneDAO"/>

    </bean>

    <bean id="phoneAction" class="org.easyphones.phonestore.action.PhoneAction">

        <property name="catalogService" ref="catalogService"/>

        <property name="phoneService" ref="phoneService"/>

    </bean>

    <bean id="shoppingAction" class="org.easyphones.phonestore.action.ShoppingAction">

        <property name="phoneService" ref="phoneService"/>

        <property name="orderService" ref="orderService"/>

    </bean>

    <bean id="orderDAO" class="org.easyphones.phonestore.dao.impl.OrderDAO" parent="baseDAO"/>

    <bean id="orderService" parent="txTemplate">

        <property name="target">

            <bean class="org.easyphones.phonestore.service.impl.OrderService">

                <property name="orderDAO" ref="orderDAO"/>

            </bean>

        </property>

    </bean>

    <!-- 定义一个事务管理器 -->

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    <!-- 定义事务管理策略 -->

    <bean id="txTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">

        <property name="transactionManager" ref="transactionManager"/>

        <property name="transactionAttributes">

            <props>

                <prop key="saveOrder">PROPAGATION_REQUIRED</prop>

            </props>

        </property>

    </bean>

</beans>

5.3 HIbernate配置文件

5.3.1 Phone.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="org.easyphones.phonestore.vo.Phone" table="phone" catalog="phonestore">

        <id name="phoneid" type="java.lang.Integer">

            <column name="phoneid" />

            <generator class="native" />

        </id>

        <many-to-one name="catalog" class="org.easyphones.phonestore.vo.Catalog" fetch="select">

            <column name="catalogid" not-null="true" />

        </many-to-one>

        <property name="phonename" type="java.lang.String">

            <column name="phonename" length="20" not-null="true" />

        </property>

        <property name="price" type="java.lang.Integer">

            <column name="price" not-null="true" />

        </property>

        <property name="picture" type="java.lang.String">

            <column name="picture" length="30" not-null="true" />

        </property>

        <set name="orderitems" inverse="true">

            <key>

                <column name="phoneid" not-null="true" />

            </key>

            <one-to-many class="org.easyphones.phonestore.vo.Orderitem" />

        </set>

    </class>

</hibernate-mapping>

5.3.2 User.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="org.easyphones.phonestore.vo.User" table="user" catalog="phonestore">

        <id name="userid" type="java.lang.Integer">

            <column name="userid" />

            <generator class="native" />

        </id>

        <property name="username" type="java.lang.String">

            <column name="username" length="20" not-null="true" />

        </property>

        <property name="password" type="java.lang.String">

            <column name="password" length="20" not-null="true" />

        </property>

        <property name="sex" type="java.lang.String">

            <column name="sex" length="4" />

        </property>

        <property name="age" type="java.lang.Integer">

            <column name="age" />

        </property>

        <set name="orderses" inverse="true">

            <key>

                <column name="userid" not-null="true" />

            </key>

            <one-to-many class="org.easyphones.phonestore.vo.Orders" />

        </set>

    </class>

</hibernate-mapping>

5.3.3 Order.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="org.easyphones.phonestore.vo.Orders" table="orders" catalog="phonestore">

        <id name="orderid" type="java.lang.Integer">

            <column name="orderid" />

            <generator class="native" />

        </id>

        <many-to-one name="user" class="org.easyphones.phonestore.vo.User" fetch="select">

            <column name="userid" not-null="true" />

        </many-to-one>

        <property name="orderdate" type="java.sql.Timestamp">

            <column name="orderdate" length="19" not-null="true" />

        </property>

     

        <set name="orderitems" cascade="all" inverse="true">

            <key>

                <column name="orderid" not-null="true" />

            </key>

            <one-to-many class="org.easyphones.phonestore.vo.Orderitem" />

        </set>

    </class>

</hibernate-mapping>

5.3.4 Orderitem.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="org.easyphones.phonestore.vo.Orderitem" table="orderitem" catalog="phonestore">

        <id name="orderitemid" type="java.lang.Integer">

            <column name="orderitemid" />

            <generator class="native" />

        </id>

        <many-to-one name="phone" class="org.easyphones.phonestore.vo.Phone" fetch="select">

            <column name="phoneid" not-null="true" />

        </many-to-one>

        <many-to-one name="orders" class="org.easyphones.phonestore.vo.Orders" fetch="select">

            <column name="orderid" not-null="true" />

        </many-to-one>

        <property name="quantity" type="java.lang.Integer">

            <column name="quantity" not-null="true" />

        </property>

         <property name="status" type="java.lang.Integer">

            <column name="status" not-null="true" />

        </property>

          <property name="comment" type="java.lang.String">

            <column name="comment" length="20" not-null="false" />

        </property>

    </class>

</hibernate-mapping>

5.3.5 Catalog.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="org.easyphones.phonestore.vo.Catalog" table="catalog" catalog="phonestore">

        <id name="catalogid" type="java.lang.Integer">

            <column name="catalogid" />

            <generator class="native" />

        </id>

        <property name="catalogname" type="java.lang.String">

            <column name="catalogname" length="20" not-null="true" />

        </property>

         <property name="catalogimg" type="java.lang.String">

            <column name="catalogimg" length="20" not-null="true" />

        </property>

        <set name="phones" inverse="true">

            <key>

                <column name="catalogid" not-null="true" />

            </key>

            <one-to-many class="org.easyphones.phonestore.vo.Phone" />

        </set>

    </class>

</hibernate-mapping>

5.3.5 Admin.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="org.easyphones.phonestore.vo.Admin" table="admin" catalog="phonestore">

        <id name="id" type="java.lang.Integer">

            <column name="id" />

            <generator class="native" />

        </id>

        <property name="username" type="java.lang.String">

            <column name="username" length="20" not-null="true" />

        </property>

        <property name="password" type="java.lang.String">

            <column name="password" length="20" not-null="true" />

        </property>

    </class>

</hibernate-mapping>

5.4 Struts2.xml文件

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="struts" extends="struts-default">

        <action name="register" class="userAction" method="register" >

            <result name="success">/register_success.jsp</result>

        </action>

        <action name="login" class="userAction">

            <result name="success">/login_success.jsp</result>

            <result name="error">/login.jsp</result>

        </action>

        <action name="logout" class="userAction" method="logout">

            <result name="success">/index.jsp</result>

        </action>

        <action name="browseCatalog" class="phoneAction" method="browseCatalog">

            <result name="success">/menu.jsp</result>

        </action>

        <action name="browsePhone" class="phoneAction" method="browsePhone">

            <result name="success">/browsePhone.jsp</result>

        </action>

   

        <action name="browsePhonePaging" class="phoneAction" method="browsePhonePaging">

            <result name="success">/browsePhonePaging.jsp</result>

        </action>

        <action name="searchPhone" class="phoneAction" method="searchPhone">

            <result name="success">/searchPhone_result.jsp</result>

        </action>

        <action name="searchPhoneByPrice" class="phoneAction" method="searchPhoneByPrice">

            <result name="success">/searchPhone_result.jsp</result>

        </action>

        <action name="addToCart" class="shoppingAction" method="addToCart">

            <result name="success">/addToCart_success.jsp</result>

        </action>

        <action name="queryorders" class="shoppingAction" method="queryorders">

            <result name="success">/showorders.jsp</result>

        </action>

        <action name="queryorderitems" class="shoppingAction" method="queryorderitems">

            <result name="success">/showorderitems.jsp</result>

        </action>

        <action name="updateCart" class="shoppingAction" method="updateCart">

            <result name="success">/showCart.jsp</result>

        </action>

        <action name="checkout" class="shoppingAction" method="checkout">

            <result name="success">/checkout_success.jsp</result>

            <result name="error">/login.jsp</result>

        </action>

        <action name="comment" class="shoppingAction" method="comment">

            <result name="success">/showorderdetail.jsp</result>

        </action>

        <action name="modifystatus" class="shoppingAction" method="modifystatus">

            <result name="success">/showorderdetail.jsp</result>

        </action>

        <action name="modifyphone" class="phoneAction" method="browsePhone">

            <result name="success">/modifyphone.jsp</result>

        </action>

            <action name="modifyphone_handle" class="phoneAction" method="modifyphone_handle">

            <result name="success">/modifyphone_call.jsp</result>

        </action>

        <action name="addphone_handle" class="phoneAction" method="addphone_handle">

            <result name="success">/modifyphone_call.jsp</result>

        </action>

        <action name="deletephone_handle" class="phoneAction" method="deletephone_handle">

            <result name="success">/modifyphone_call.jsp</result>

        </action>

    </package>

</struts>

标签: 手机

实例下载地址

手机网店

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 helloweb 发表于: 2017-07-30 18:11 44
下载请注意,是论文文档。

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警