1.Struts.jar 2.commons-beanutils.jar 3.commons-digester.jar
4. commons-collections.jar 5. commons-logging.jar 6.Hibernate3.jar
7. antlr-
10. dom4j-
14. log4j- |
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>Struts Demo</title>
</head>
<body>
<H1>Hell, ${userInfo["username"]} !</H1>
</body>
</html> |
create table us (id int(10) primary key auto_increment, name varchar(20))type=innodb;
insert into us (name) values("zhaoyuan"); |
package edu.bjtu.zhao.ssh;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} |
package edu.bjtu.zhao.ssh;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class UserDAO {
private SessionFactory sessionFactory;
public UserDAO() {
}
public UserDAO(SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public User find(Integer id) {
Session session = sessionFactory.openSession();
User user = (User) session.load(User.class, id);
Hibernate.initialize(user);
session.close();
return user;
} } |
package edu.bjtu.zhao.ssh;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloAction extends Action {
// private UserChecker userChecker;
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String username = request.getParameter("user");
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//建立DAO对象
UserDAO userDAO = (UserDAO) context.getBean("userDAO");
User user = new User();
user = userDAO.find(new Integer(1));
if(username==null )
{
username=user.getName();
}
Map model = new HashMap();
model.put("username", username);
request.setAttribute("userInfo", model);
return mapping.findForward("helloUser");
} } |
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!—配置Spring的 applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
</web-app> |
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action
path="/hello"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward
name="helloUser"
path="/WEB-INF/jsp/hello.jsp"/>
</action>
</action-mappings>
<!-- 因为使用了代理类,DelegatingActionProxy,注册一个插件,让它通过/Hello去寻找真正的实现类 -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml"/>
</plug-in>
</struts-config>
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="userDAO" class="edu.bjtu.zhao.ssh.UserDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>hibernate.cfg.xml</value>
</property>
</bean>
</beans> |
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 与struts-config.xml里的"/hello"对应,由代理类返回HelloActoine类
到此spring 与struts配置完毕,也就是还说spring只是管理了action
-->
<bean name="/hello"
class="edu.bjtu.zhao.ssh.HelloAction">
</bean>
</beans> |
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--是否使运行时生成的sql输出到控制台以供调试 -->
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sample
</property>
<property name="connection.username">root</property>
<property name="connection.password">
</property>
<!-- 以下设置对象与数据库表格的映射文件(所用用到的hbm文件在此配置),必须是相对于根的全路径 -->
<mapping resource="edu/bjtu/zhao/ssh/User.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="edu.bjtu.zhao.ssh.User"
table="user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
</class>
</hibernate-mapping> |
<Context path="/SSHDemo" docBase="D:\j2ee\workspace\SSHDemo\WebRoot" reloadable="true" debug="0">
</Context> |