一、新建项目
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