maven搭建myeclipse10+struts2.1+spring3.3+hibernate3.3全注解框架

gjggw123 贡献于2013-12-07

作者 Microsoft  创建于2013-06-08 06:59:00   修改者Sky123.Org  修改于2013-06-20 12:56:00字数9451

文档摘要:maven搭建myeclipse10+struts2.1+spring3.3+hibernate3.3全注解框架
关键词:

一、 新建项目 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; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; /** * @author steve frank * */ @ParentPackage("struts-default") public class LoginAction extends ActionSupport { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void 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") }) public String execute() { if (name.equals("steve") && password.equals("123")) return SUCCESS; else return INPUT; } } Index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> My JSP 'index.jsp' starting page success.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> My JSP 'success.jsp' starting page
fail.jsp 登录失败,用户名或密码错误. 三、 添加hibernate框架 1. 添加hibernate支持 右键->myeclipse->add hibernate capabilities 点选hibernate 3.3 勾选enable hibernate annotations support 勾选hibernate 3.3 annotations&entity manager,hibernate 3.3 core lib,hibernate 3.3 advanced support lib 点选copy checked library jars to folder and 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.0 persistence jdbc; 勾选copy checked library contents to project folder 采取默认,enable aop,new applicationcontext.xml 不创建sessionfactory 2. applicationContext.xml文件配置 3. hibernate.cfg.xml修改 org.hibernate.dialect.MySQLDialect jdbc:mysql://localhost:3306/test root admin com.mysql.jdbc.Driver MYSQL 5 100 30 500 false 4. web.xml文件配置 index.jsp contextConfigLocation classpath*:applicationContext*.xml org.springframework.web.context.ContextLoaderListener struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* Set Character Encoding org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true Set Character Encoding /* 五、 类的添加 1. hibernate反向生成 students表右键->hibernate reverse engineering 选择package : com.inscribe.farsighted.model 勾选create pojo<>db->点选add hibernate mapping annotations to pojo;勾选update hibernate configuration with mapping resource location 勾选java data access object(dao)->勾选generate precise findby methods;点选sping dao Id generation :native 2. 添加service接口 package org.inscribe.farsighted.service; import org.inscribe.farsighted.model.Student; public interface StudentService { public Student findById(Integer id); } 3. 添加service实现类 package org.inscribe.farsighted.service.impl; import javax.annotation.Resource; import org.inscribe.farsighted.model.Student; import org.inscribe.farsighted.model.StudentDAO; import org.inscribe.farsighted.service.StudentService; import org.springframework.stereotype.Service; @Service public class StudentServiceImpl implements StudentService { @Resource private StudentDAO studentDAO; @Override public Student findById(Integer id) { // TODO Auto-generated method stub return studentDAO.findById(id); } } 4. 添加测试类 package org.inscribe.farsighted; import org.inscribe.farsighted.model.Student; import org.inscribe.farsighted.service.StudentService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); StudentService service = (StudentService) context.getBean("studentServiceImpl"); Student s=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 private StudentService studentService; 测试 Student s = studentService.findById(1); System.out.println(s.getName()); 10. success.jsp中添加
${password} 11. 启动tomcat,地址栏输入http://localhost:8088/farsighted/ 点击submit效果如下 包结构如下 12. 测试完成.

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 6 金币 [ 分享文档获得金币 ] 1 人已下载

下载文档