WEBAPP开发教程之Maven搭建MyEclipse10+Struts2.1+Spring3.3+Hibernate3.3全注解框架
凌雪 2018-10-08 来源 :网络 阅读 930 评论 0

摘要:本文将带你了解WEBAPP开发教程Maven搭建MyEclipse10+Struts2.1+Spring3.3+Hibernate3.3全注解框架,希望本文对大家学WEBAPP有所帮助。

本文将带你了解WEBAPP开发教程Maven搭建MyEclipse10+Struts2.1+Spring3.3+Hibernate3.3全注解框架,希望本文对大家学WEBAPP有所帮助。


一、新建项目
    1.        新建maven项目
    New ->project
    
    选择Maven project
    
    next
    
    在filter输入webapp
    
    选择maven-archetype-webapp
    
    Group id输入inscribe,artifact id输入farsighted
    
    二、添加struts2框架
    1.        添加struts2支持
    项目上点右键->myeclipse->add struts capabilities
    
    点选struts2.1和/*
    
    只勾选struts2 core lib
    
    启动tomcat,在地址栏输入localhost:8088/farsighted出现如下界面
    
    2.        添加action类
    New ->source folder
    
    输入src/main/java
    
    New ->class
    
    Package输入org.inscribe.farsighted.action,name输入LoginAction
    
    LoginAction.java
    package org.inscribe.farsighted.action;
     
    importorg.apache.struts2.convention.annotation.Action;
    importorg.apache.struts2.convention.annotation.ParentPackage;
    importorg.apache.struts2.convention.annotation.Result;
    import com.opensymphony.xwork2.ActionSupport;
     
    /**
     *@author steve frank
     * 
     */
    @ParentPackage("struts-default")
    public class LoginAction extendsActionSupport {
             privateString name;
             privateString   password;
     
             publicString getName()   {
                           returnname;
             }
     
             publicvoid setName(String   name) {
                        this.name = name;
             }
     
             publicString getPassword()   {
                           returnpassword;
             }
     
             publicvoid   setPassword(String password) {
                         this.password= password;
             }
     
             @Action(value=   "login", results = {
                                  @Result(name= INPUT, location = "/WEB-INF/content/fail.jsp"),
                                  @Result(name= SUCCESS, location = "/WEB-INF/content/success.jsp")   })
             publicString execute()   {
                           if(name.equals("steve") && password.equals("123"))
                                    returnSUCCESS;
                         else
                                    returnINPUT;
             }
    }
    Index.jsp
    <%@ page="" language="java" import="java.util.*" pageencoding="UTF-8">
    <%@ taglib="" prefix="s" uri="/struts-tags">
    
   
   
   
   
   
               
                           
                           
                         
             
   
   
    success.jsp
    <%@ page="" language="java" import="java.util.*" pageencoding="UTF-8">
    <%@ taglib="" prefix="s" uri="/struts-tags">
    
   
   
     
   
   
     
   
               
            


              


   


   
    fail.jsp
   登录失败,用户名或密码错误.
   
    
    三、添加hibernate框架
    1.        添加hibernate支持
    右键->myeclipse->add hibernate capabilities
    
    点选hibernate 3.3 勾选enable hibernate annotations support
    勾选hibernate3.3 annotations&entity manager,hibernate 3.3 core   lib,hibernate 3.3 advancedsupport lib
    点选copy checked library jars to folderand add to build-path
    
    点选new
    Folder :src/main/resources
    勾选open configuration file
    
    选择db driver:mysql
    
    去掉勾,不创建hibernatesessionfactory
    
    四、添加spring框架
    1.        添加spring支持
    项目右键->myeclipse->add spring capabilities
    
    点选spring3.0
    勾选spring 3.0 core;spring 3.0 aop;spring3.0 persistence core;   spring3.0persistence jdbc;
    勾选copy checked library contents to project folder
    
    采取默认,enable aop,new applicationcontext.xml
    
    不创建sessionfactory
    
      2.        applicationContext.xml文件配置
   


   <beansxmlns="//www.springframework.org/schema/beans"
               xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"xmlns:context="//www.springframework.org/schema/context"
               xmlns:aop="//www.springframework.org/schema/aop"xmlns:tx="//www.springframework.org/schema/tx"
             xsi:schemaLocation="
                 //www.springframework.org/schema/beans
                 //www.springframework.org/schema/beans/spring-beans.xsd
                 //www.springframework.org/schema/context
                 //www.springframework.org/schema/context/spring-context.xsd
                 //www.springframework.org/schema/aop
                 //www.springframework.org/schema/aop/spring-aop.xsd
                 //www.springframework.org/schema/tx
                 //www.springframework.org/schema/tx/spring-tx.xsd">
     
     
            <bean   id="sessionFactory"
                           class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                          


                          


                        


            
     
             
            
     
             
              <beanid="transactionManager"
                         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                          
            
     
             
              <tx:annotation-driventransaction-manager="transactionManager"
                         proxy-target-class="true"/>
     
   
    3.        hibernate.cfg.xml修改
    
    <!DOCTYPEhibernate-configuration PUBLIC
                "-//Hibernate/HibernateConfiguration DTD 3.0//EN"
               "//hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     
    
   
     
            
                          
                                    org.hibernate.dialect.MySQLDialect
                          
                          
                                    jdbc:mysql://localhost:3306/test
                          
                          root
                        admin
                       
                                    com.mysql.jdbc.Driver
                        
                          MYSQL
                           
                           
                          5
                          100
                          30
                        500
                          false
              
     
   
    4.        web.xml文件配置
    
   <web-appversion="3.0"   xmlns="//java.sun.com/xml/ns/javaee"
             xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="//java.sun.com/xml/ns/javaee
               //java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
              
              
                        index.jsp
              
            
                          contextConfigLocation
                        
                                    classpath*:applicationContext*.xml
                          
            
     
            
                          org.springframework.web.context.ContextLoaderListener
            
            
                        struts2
                          
                                  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
                        
            
            
                          struts2
                        /*
              
     
            
                          SetCharacter Encoding
                        org.springframework.web.filter.CharacterEncodingFilter
                          
                                   encoding
                                 UTF-8
                        
                          
                                 forceEncoding
                                 true
                          
            
              
                        SetCharacter Encoding
                          /*
              
   
    五、类的添加
    1.      hibernate反向生成
    students表右键->hibernate reverse engineering
    
    选择package : com.inscribe.farsighted.model
    勾选create pojo<>db->点选add hibernatemapping annotations to   pojo;勾选update hibernate configuration with mapping resource location
    勾选java data access object(dao)->勾选generate precisefindby methods;点选sping   dao
    
    Id generation :native
    
    2.      添加service接口
    packageorg.inscribe.farsighted.service;
     
    importorg.inscribe.farsighted.model.Student;
     
    public interfaceStudentService {
             public Student   findById(Integer id);
    }
    3.      添加service实现类
    package org.inscribe.farsighted.service.impl;
     
    importjavax.annotation.Resource;
     
    importorg.inscribe.farsighted.model.Student;
    importorg.inscribe.farsighted.model.StudentDAO;
    importorg.inscribe.farsighted.service.StudentService;
    importorg.springframework.stereotype.Service;
     
    @Service
    public classStudentServiceImpl implements StudentService {
             @Resource
             private StudentDAO   studentDAO;
     
             @Override
             public Student   findById(Integer id) {
                         // TODO Auto-generated methodstub
                           returnstudentDAO.findById(id);
             }
     
    }
    4.      添加测试类
    packageorg.inscribe.farsighted;
     
     
    importorg.inscribe.farsighted.model.Student;
    importorg.inscribe.farsighted.service.StudentService;
    importorg.springframework.context.ApplicationContext;
    import   org.springframework.context.support.ClassPathXmlApplicationContext;
     
    public classTest {
     
             public static void   main(String[] args){
                           ApplicationContext context =new ClassPathXmlApplicationContext(
                                             "applicationContext.xml");
                           StudentService service =(StudentService)   context.getBean("studentServiceImpl");
                           Students=service.findById(1);
                           System.out.println(s.getName());
             }
    }
    5.      WEB-INF下新建Folder
    包结构如下
    
    6.      修改输出目录
    Build path->configure build path
    
    Out folder统一改default:Webapp/src/main/webapp/WEB-INF/classes
    
    更改如果无效,在pom.xml中……添加
   src/main/java
                          
                                   
                                            src/main/resources
                                   
                          
                          src/main/webapp/WEB-INF/classes
    7.      添加struts-spring支持(action注解配置支持)
    Build path->add libraries
    
    Myeclipse libraries
    
    勾选struts 2 spring libraries
    
    8.      修改struts.xml
    
    

              
   
      9.      在LoginAction.java中添加调用StudentService
    @Resource
             privateStudentService   studentService;
             
             测试
             Students =   studentService.findById(1);
    System.out.println(s.getName());
    10.  success.jsp中添加   ${password}
    11.  启动tomcat,地址栏输入//localhost:8088/farsighted/
    
    点击submit效果如下
    
    包结构如下
    
    12.   测试完成.
     文库地址://wenku.baidu.com/view/454c015fe45c3b3567ec8b3c.html
             
         


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之WebApp频道!


本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程