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;
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"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' startingpage</title>
</head>
<body>
<s:formaction="login">
<s:textfieldname="name" value="steve"></s:textfield>
<s:textfieldname="password" value="123"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
success.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s"uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'success.jsp' startingpage</title>
</head>
<body>
<s:textname="password" />
<hr/>
<s:propertyvalue="password" />
</body>
</html>
fail.jsp
<body>登入失敗,使用者名稱或密碼錯誤.
</body>
三、新增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檔案配置
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"value="classpath:hibernate.cfg.xml">
</property>
<propertyname="packagesToScan" value="org.inscribe.farsighted.model"/>
</bean>
<!-- 使用 annotation 自動註冊bean,並檢查@Controller,@Service, @Repository註解已被注入 -->
<context:component-scan base-package="org.inscribe.farsighted"/>
<!-- 配置事務管理 -->
<beanid="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置註解實現管理事務(cglib:proxy-target-class="true") -->
<tx:annotation-driventransaction-manager="transactionManager"
proxy-target-class="true"/>
</beans>
3. hibernate.cfg.xml修改
<?xmlversion='1.0' encoding='UTF-8'?>
<!DOCTYPEhibernate-configuration PUBLIC
"-//Hibernate/HibernateConfiguration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<propertyname="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<propertyname="connection.username">root</property>
<propertyname="connection.password">admin</property>
<propertyname="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<propertyname="myeclipse.connection.profile">MYSQL</property>
<!-- <mappingclass="org.inscribe.farsighted.model.Student" /> -->
<!-- Connection PoolingInfo -->
<propertyname="initialSize">5</property>
<propertyname="maxActive">100</property>
<propertyname="maxIdle">30</property>
<propertyname="maxWait">500</property>
<propertyname="defaultAutoCommit">false</property>
</session-factory>
</hibernate-configuration>
4. web.xml檔案配置
<?xmlversion="1.0" encoding="UTF-8"?>
<web-appversion="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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SetCharacter Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacter Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
五、類的新增
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中<build>……</build>新增
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
7. 新增struts-spring支援(action註解配置支援)
Build path->add libraries
Myeclipse libraries
勾選struts 2 spring libraries
8. 修改struts.xml
<?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPEstruts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 開啟使用開發模式,詳細錯誤提示-->
<constantname="struts.devMode" value="true" />
<!-- 將物件交給spring管理 -->
<constantname="struts.objectFactory" value="spring" />
<!-- 指定資源編碼型別 -->
<constantname="struts.i18n.encoding" value="UTF-8" />
<!-- 指定每次請求到達,重新載入資原始檔-->
<constantname="struts.i18n.reload" value="true" />
<!-- 指定每次配置檔案更改後,自動重新載入-->
<constantname="struts.configuration.xml.reload" value="false" />
<!-- 國際化資原始檔 -->
<constantname="struts.custom.i18n.resources"value="content/Language" />
<!-- 瀏覽器快取 -->
<constantname="struts.serve.static.browserCache" value="false" />
<!-- 預設字尾名 -->
<constantname="struts.action.extension" value="do,action,jhtml,,"/>
<!-- Struts Annotation -->
<constantname="actionPackages" value="com.frank.action" />
</struts>
9. 在LoginAction.java中新增呼叫StudentService
@Resource
privateStudentService studentService;
測試
Students = studentService.findById(1);
System.out.println(s.getName());
10. success.jsp中新增<hr/> ${password}
11. 啟動tomcat,位址列輸入http://localhost:8088/farsighted/
點選submit效果如下
包結構如下
12. 測試完成.
文庫地址:http://wenku.baidu.com/view/454c015fe45c3b3567ec8b3c.html