首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ssh环境的筹建

2012-10-09 
ssh环境的搭建一.加载所有的sshjar包二..ssh的所有配制文件:1.spring文件的配制:applicationContext-actio

ssh环境的搭建
一.加载所有的sshjar包

二..ssh的所有配制文件:
     1.spring文件的配制:
         applicationContext-actions.xml中内容:
         <?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: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-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
         
    <bean name="/user" scope="prototype">
    <property name="userManager" ref="userManager"/>
    </bean>
    <bean name="/cource" scope="prototype">
    <property name="userManager" ref="userManager"/>
    <property name="courceManager" ref="courceManager"/>
    </bean>
</beans>

applicationContext-beans.xml文件中的内容:
<?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: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-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="userManager" ref="sessionFactory"></property>
</bean>
<bean id="courceManager" ref="sessionFactory"></property>
</bean>
</beans>

applicationContext-common.xml文件中的内容:
<?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: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-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置sessionFactory -->
<bean id="sessionFactory" transaction-manager="transactionManager">
    <tx:attributes>
       <tx:method name="add*" propagation="REQUIRED"/>
       <tx:method name="del*" propagation="REQUIRED"/>
       <tx:method name="update*" propagation="REQUIRED"/>
       <tx:method name="*" read-only="true"/>
    </tx:attributes>
    </tx:advice>
  
    <!-- 配置那些类的方法进行事务管理 -->
    <aop:config>
    <aop:pointcut id="allManagerMethod" expression="execution (* com.hbin.homepage.manager.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
    </aop:config>         
</beans>

2.hibernate文件的配制:
    hibernate.cfg.xml文件中的内容:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/homepage</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!--实体对象配制-->
    <mapping resource="com/hbin/homepage/model/User.hbm.xml"/>
    <mapping resource="com/hbin/homepage/model/Cource.hbm.xml"/>
   </session-factory>
</hibernate-configuration>

   User.hbm.xml映射文件的内容:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class table="t_user" name="com.hbin.homepage.model.User">
    <id name="id">
      <generator not-null="true" unique="true"/>
    <property name="realName"/>
    <property name="pd"/>
    <property name="url"/>
    <property name="rdate" update="false"/>
         <!-- 先保存User再保存Cource -->
   <!-- 这里order-by是指把得到的集合元素按id升序排列,也可以order-by="id desc"是降序.只有排序好了
   在前台展现的时候才不会出现数据的顺序乱变的情况-->
   <set inverse="true" name="cources" lazy="false" order-by="id">
      <key column="userId" on-delete="cascade"/>
   <one-to-many name="gdesigns" lazy="false" order-by="id">
      <key column="userId" on-delete="cascade"/>
   <one-to-many encoding="UTF-8"?>
<web-app 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
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<welcome-file-list>
   <welcome-file>index.htm</welcome-file>
</welcome-file-list>
<!-- Standard Action Servlet Configuration (with debugging) -->
<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>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 加载spring的配制文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配制Spring对hibernate session开关的过滤器的支持(其实它的实现和我们以前用的那种在web.xml文件中
配制property name="current_session_context_class">thread</property再在调用getCurrentSession()
这个方法,一样的效果) -->
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring对页面编码字符集过滤器的支持 -->
 <filter>
    <filter-name>Spring character encoding filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>GBK</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>Spring character encoding filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

    <!-- 这个servlet是自动加载的,所以没有映射的url -->
<servlet>
    <servlet-name>utilDateConverter</servlet-name>
    <servlet-class>com.hbin.homepage.servlet.UtilDateConverterInitServlet</servlet-class>
    <load-on-startup>3</load-on-startup>
</servlet>
<!-- dwr框架配制 -->
<servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

<!-- 分页处理的过滤器 -->
<filter>
    <filter-name>pagerFilter</filter-name>
    <filter-class>com.hbin.homepage.servlet.PagerFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>pagerFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 访问权限控制,注意这里配制了两个mapping,如果有需要的话可以配制多个.这里第二个mapping就是不能
打开http://localhost:8080/THomePageService2.4/index.jsp这个连接,因为那样前台的首页就没有用户对象
展现的时候就没有数据了.所以限制用户用http://localhost:8080/THomePageService2.4/user.do?method=allUsersInfo&target=front
这个连接来访问这个这个前台的index.jsp页面.当然在filter的Java文件我们还需要判断处理.-->
<filter>
    <filter-name>AuthFilter</filter-name>
    <filter-class>com.hbin.homepage.servlet.AuthFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>AuthFilter</filter-name>
    <url-pattern>/admin/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>AuthFilter</filter-name>
    <url-pattern>/index.jsp</url-pattern>
</filter-mapping>

</web-app>

struts-config.xml文件的配制内容:
<?xml version="1.0" encoding="GBK" ?>

<!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>
<form-beans>
   <form-bean name="userActionForm" type="com.hbin.homepage.web.form.UserActionForm"></form-bean>
   <form-bean name="courceActionForm" type="com.hbin.homepage.web.form.CourceActionForm"></form-bean>
   <form-bean name="gdesignActionForm" type="com.hbin.homepage.web.form.GDesignActionForm"></form-bean>
   </form-beans>
<!-- 全局标签的配制要放在action-mappings前面 -->
<!-- 这里的scope="request"就是把这个异常信息放到request中.这里的key值没用的,真正的key是
在hander类中读出来的.这里的path没有写的话就会用input的值作为路经.这里的key的值是当我们在抛出的
异常中没有key的话它就会用这个默认的key来处理,这个key的值在资源文件中也是有配制的. -->
<global-exceptions>
   <exception
    key="allerrors.default"
    type="java.lang.Exception"
    path="/common/exception.jsp"
    scope="request"
    handler="com.hbin.homepage.util.SystemExceptionHandler"
   ></exception>
</global-exceptions>
<!--
<global-forwards>
   <forward name="pub_add_success" path="/common/pub_add_success.jsp"></forward>
   <forward name="pub_update_success" path="/common/pub_update_success.jsp"></forward>
   <forward name="pub_del_success" path="/common/pub_del_success.jsp"></forward>
</global-forwards>
-->
<action-mappings>
   <action
    path="/user"
    type="org.springframework.web.struts.DelegatingActionProxy"
    name="userActionForm"
    scope="request"
    parameter="method"
   >
    <forward name="register" path="/register.htm"/>
    <forward name="index" path="/index.jsp"/>
    <forward name="login" path="/login.htm"/>
    <forward name="admin" path="/admin/index.jsp" redirect="true"/>
   </action>
 
    </action-mappings>
    <message-resources parameter="MessageResources" />
</struts-config>

struts中action和form的编写:
package com.hbin.homepage.web.action;

import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import org.apache.commons.beanutils.BeanUtils;//注意这个类不是spring中的那个类

import com.hbin.homepage.manager.UserManager;
import com.hbin.homepage.model.User;
import com.hbin.homepage.util.PagerModel;
import com.hbin.homepage.util.SystemException;
import com.hbin.homepage.web.form.UserActionForm;

public class UserAction extends DispatchAction {
private UserManager userManager;


@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

   return super.unspecified(mapping, form, request, response);
}
//添加用户输入
public ActionForward addUserInput(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
   return mapping.findForward("register");
}
//添加用户(用户注册)
public ActionForward addUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
   UserActionForm uaf = (UserActionForm)form;
   User user = new User();
   BeanUtils.copyProperties(user,uaf);
   user.setRdate(new Date());
   int id = userManager.addUser(user);//保存了这个user,有了这个user的id才好设置url
   //不是这个地址:http://localhost:8080/THomePageService1.1/homepage.jsp
   //http://localhost:8080/THomePageService1.9/enterhomepage.do?method=enterUserHomePage&id=34
   String url = request.getScheme()+"://"+request.getServerName()+":"
    +request.getServerPort()+request.getContextPath()
    +"/enterhomepage.do?method=enterUserHomePage&userId="+id;
   user.setUrl(url);
   //System.out.println("--" + user.getId());//这里也可以得到才保存的那个用户的id
 
   userManager.updateUser(user);
   return mapping.findForward("index");
}
//-------------------------------------------------------



public void setUserManager(UserManager userManager) {
   this.userManager = userManager;
}
}

struts中的form的编写:
package com.hbin.homepage.web.form;

import java.util.Date;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class UserActionForm extends ActionForm {
private int id;
private String name;
private String realName;
private String pd;

//是不是管理员
private String isAdmin;
//头像图片上传
private FormFile file;

private String target;

public int getId() {
   return id;
}
public void setId(int id) {
   this.id = id;
}
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
public String getPd() {
   return pd;
}
public void setPd(String pd) {
   this.pd = pd;
}
public String getUrl() {
   return url;
}
public void setUrl(String url) {
   this.url = url;
}
public String getSex() {
   return sex;
}
public void setSex(String sex) {
   this.sex = sex;
}
public void setTarget(String target) {
   this.target = target;
}
}

热点排行