最近准备将工作平台由 MyEclipse 迁移到 NetBeans 开发新的项目,同时升级我的框架。MyEclipse 的收费太麻烦了,我个人越来越讨厌到处找 SN 所以决定抛弃之,选用 NetBeans 这个免费的开源 IDE 作为主开发环境。
1.获取方式:
最新版本的 Netbeans 是 6.9.1 版本,下载 JAVA 支持的版本,IDE 中集成了 TomCat 6.0.26.
http://netbeans.org/downloads/index.html
2.安装完成后,配置需要的框架。
- Hibernate3 IDE集成,无特殊需要不需要配置
- Spring3 IDE集成,无特殊需要不需要配置
3.使用 NetBeans 完成 完整的 基于 Struts2 Spring3 Hibernate3 的用户登录 Demo
- 准备数据库,这个实例应用 MySQL
DROP DATABASE IF EXISTS `user`; CREATE DATABASE `user` USE `user`; DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(255) DEFAULT NULL, `userPass` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES (1,'test','test');
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!--为Spring3添加配置文件路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param> <!--为Struts2添加过滤器--> <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> <!--为Spring3添加过滤器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?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" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- Spring 管理的 hibernate sessionFactory --> <!--读取 /WEB-INF/jdbc.properties 文件获得数据库连接信息--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties" /> <!--配置 DataSource 数据源--> <bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> <!--配置 SessionFactory --> <bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="DataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.current_session_context_class"> org.springframework.orm.hibernate3.SpringSessionContext </prop> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> <property name="mappingDirectoryLocations"> <list> <!--Hibernate3 实体映射文件的读取采用了读取整个包内实体的方式--> <value> classpath:/com/track2web/demo/entity </value> </list> </property> </bean> <bean id="HibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="SessionFactory" /> </property> </bean> <!--自动事物配置--> <bean id="TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="SessionFactory"/> </bean> <!--指定事务策略,声明一个通知,用以指出要管理哪些事务方法及如何管理--> <tx:advice id="txAdvice" transaction-manager="TransactionManager"> <tx:attributes> <!-- 对get/find开头的方法要求只读事务 --> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <!-- 对其它方法要求事务 --> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!--声明一个config,用以将事务策略和业务类关联起来--> <aop:config> <!-- 添加事务支持,因为前面配置的transactionManager是专对Hibernate的事务管理器--> <aop:pointcut id="bizMethods" expression="execution(* com.track2web.demo.service.*.*(..))" /> <!-- 织入 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" /> </aop:config> </beans>
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/user jdbc.username=root jdbc.password=1234
package com.track2web.demo.entity; public class User implements java.io.Serializable { private Integer id; private String userName; private String userPass; public User() { } public User(String userName, String userPass) { this.userName = userName; this.userPass = userPass; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPass() { return this.userPass; } public void setUserPass(String userPass) { this.userPass = userPass; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.track2web.demo.entity.User" table="user" catalog="user"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="identity" /> </id> <property name="userName" type="string"> <column name="userName" /> </property> <property name="userPass" type="string"> <column name="userPass" /> </property> </class> </hibernate-mapping>
package com.track2web.demo.dao; import com.track2web.demo.entity.User; public interface IUserDAO { public User login(User value); }
package com.track2web.demo.dao; import com.track2web.demo.entity.User; import java.util.List; import org.springframework.orm.hibernate3.HibernateTemplate; public class UserDAO implements IUserDAO { private HibernateTemplate hibernateTemplate; public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } @SuppressWarnings("unchecked") public User login(User value) { List<User> list = (List<User>)this.hibernateTemplate.findByExample(value); return (!list.isEmpty()?list.get(0):null); } }
<?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" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="UserDAO" class="com.track2web.demo.dao.UserDAO"> <property name="hibernateTemplate"> <ref bean="HibernateTemplate" /> </property> </bean> </beans>
package com.track2web.demo.service; import com.track2web.demo.entity.User; public interface IUserService { public User login(User value); }
package com.track2web.demo.service; import com.track2web.demo.dao.IUserDAO; import com.track2web.demo.entity.User; public class UserService implements IUserService { private IUserDAO userDAO; public IUserDAO getUserDAO() { return userDAO; } public void setUserDAO(IUserDAO userDAO) { this.userDAO = userDAO; } public User login(User value) { return this.userDAO.login(value); } }
<?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" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="UserService" class="com.track2web.demo.service.UserService"> <property name="userDAO"> <ref bean="UserDAO" /> </property> </bean> </beans>
package com.track2web.demo.action; import com.opensymphony.xwork2.ActionSupport; import com.track2web.demo.entity.User; import com.track2web.demo.service.IUserService; public class LoginAction extends ActionSupport { private User user; private IUserService userService; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public IUserService getUserService() { return userService; } public void setUserService(IUserService userService) { this.userService = userService; } @Override public String execute() { this.user = this.userService.login(user); if (user!=null) { return SUCCESS; } else { return ERROR; } } }
<?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" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="LoginAction" class="com.track2web.demo.action.LoginAction" scope="prototype" > <property name="userService"> <ref bean="UserService" /> </property> </bean> </beans>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- Configuration for the default package. --> <package name="login-demo" extends="struts-default"> <action name="login" class="LoginAction"> <result name="success">/success.jsp</result> <result name="error">/index.jsp</result> </action> </package> </struts>
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login Demo</title> </head> <body> <h1>Login Demo</h1> <s:form action="login" method="post" namespace="/login-demo"> <s:textfield name="user.userName" label="USERNAME" /> <s:password name="user.userPass" label="USERPASS" /> <s:submit value="login" /> </s:form> </body> </html>
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login Demo</title> </head> <body> <h1>Login Demo</h1> <h3> <s:property value="user.userName"/> Login Success!</h3> </body> </html>
Categories: 网页编程