简易搭建ssh 框架

 配置文件:

1.struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	
	<constant name="struts.Factory" value="spring"></constant>
	<package name="com.taoge.action" extends="struts-default" namespace="/taoge">
		<action name="findall"  class="testBean">
			<result>/success.jsp</result>
		</action>
		
		<action name="save"  class="testBean" method="save">
			<result name="SUCCESS">/success.jsp</result>
			<result name="save">/error.jsp</result>
		</action>
	</package>
	
	<package name="ajax_action" extends="json-default" namespace="/">
		
		<action name="ajax_*" class="testBean" method="{1}">
			<result type="json" ></result>
		</action>
	
	
	</package>

</struts>    

 web.xml

<?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">
  <display-name></display-name>	
  
  <context-param>
  	<param-name></param-name>
  	<param-value></param-value>
  </context-param>
  
  <context-param>
        <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
  </context-param>
  
  <!-- 管理session -->
  <filter>
  	<filter-name>lazyLoadingFilter</filter-name>
  	<filter-class>
  		org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  	</filter-class>
  	<init-param>  
            <!--  
                指定org.springframework.orm.hibernate3.LocalSessionFactoryBean  
                在spring配置文件中的名称,默认值为sessionFactory.  
                如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,  
                该参数一定要指定,否则会出现找不到sessionFactory的例外  
            -->  
            <param-name>sessionFactory</param-name>  
            <param-value>sessionFactory</param-value>  
    </init-param>
  </filter>
 
  
  <!-- Spring Listener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
	       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	
  
  <!-- Log4j Config -->
	<context-param>   
	    <param-name>log4jConfigLocation</param-name>   
	    <param-value>/WEB-INF/classes/log4j.properties</param-value>   
	</context-param> 
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
  </filter>
  
  
  
  <filter-mapping>
  	<filter-name>lazyLoadingFilter</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping>
  
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping>
  
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>*.jsp</url-pattern>
  </filter-mapping>
  
  
  <filter>
	  <filter-name>CharacterEncodingFilter</filter-name>
	  <filter-class>
	   		com.taoge.filter.FilterEncoding
	  </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>CharacterEncodingFilter</filter-name>
	  <url-pattern>/*</url-pattern>
 </filter-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  </web-app>

 3.applicationContext.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="jdbcUrl" value="jdbc:mysql://116.255.144.155:58586/360_dflq_accounts"></property>
		<property name="user" value="rylq_admin"></property>
		<property name="password" value="rylq_210621047"></property>
		
		  <!-- 连接池中保留的最小连接数.-->
		  <property name="minPoolSize">
		   <value>5</value>
		  </property>
		  <!-- 连接池中保留的最大连接数。Default: 15 -->
		  <property name="maxPoolSize">
		   <value>30</value>
		  </property>
		  <!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		  <property name="initialPoolSize">
		   <value>10</value>
		  </property>
		  <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		  <property name="maxIdleTime">
		   <value>60</value>
		  </property>
		  <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		  <property name="acquireIncrement">
		   <value>5</value>
		  </property>
		
		  <!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
		     属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
		     如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
		  <property name="maxStatements">
		   <value>0</value>
		  </property>
		  <!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
		  <property name="idleConnectionTestPeriod">
		   <value>60</value>
		  </property>
		  <!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
		  <property name="acquireRetryAttempts">
		   <value>30</value>
		  </property>
		  <!-- 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
		     保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
		     获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
		  <property name="breakAfterAcquireFailure">
		   <value>true</value>
		  </property>
		  <!-- 因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
		   
		     时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
		   
		     等方法来提升连接测试的性能。Default: false -->
		  <property name="testConnectionOnCheckout">
		   <value>false</value>
		  </property> 
		
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		   
		<property name="mappingResources">
			<list>
				<value>com/taoge/model/Account.hbm.xml</value>
				<value>com/taoge/model/Notice.hbm.xml</value>
			</list>
		</property>
		
		<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
                <prop key="hibernate.autoReconnect">true</prop>
            </props>
        </property>
		
	</bean>
	
	<bean id="stuDao" class="com.taoge.dao.hibernate3.stuHibernate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	
	</bean>
	
	<bean id="stuManager" class="com.taoge.service.Imp.stuManagerImpl">
		<property name="stuDao">
			<ref bean="stuDao"/>
		</property>
	</bean>
	
	
	<bean id="testBean" class="com.taoge.action.stuAction" scope="prototype">
		<property name="stuManager">
			<ref bean="stuManager"/>
		</property>
		<property name="noticeDAO">
			<ref bean="noticeDAO"/>
		</property>
	</bean>
	
	
	<bean id="noticeDAO" class="com.taoge.model.NoticeDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean></beans>
	
	
	
	
	
 

4.action类:

package com.taoge.action;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.taoge.model.Account;
import com.taoge.model.Notice;
import com.taoge.model.NoticeDAO;
import com.taoge.service.stuManager;


@Entity
public class stuAction extends ActionSupport  {

	/**
	 * 
	 */
	@Id
	@GeneratedValue
	private static final long serialVersionUID = 1L;
	
	@ManyToOne
	private stuManager stuManager;
	
	private NoticeDAO noticeDAO;
	
	public void setNoticeDAO(NoticeDAO noticeDAO) {
		this.noticeDAO = noticeDAO;
	}

	private List list;
	
	
	private String str="haha"; 
	private String result; 
	private Notice noticeBean; 
	
	private String haha; 
	private String command; 

	private String username;
	
	private String password; 
	
	private Account user = null; 
	
	private Account users; 
	

	



	public String add(Object obj){
		
		stuManager.add(obj);
		return SUCCESS;
	}
	
	
	public String login2(){
		
		System.out.println(user.getUsername()+":"+user.getPassword()+"哈哈:"+this.getHaha());
		
		String rslt=this.getResult();
		
		noticeBean = (Notice)JSONObject.toBean(JSONObject.fromObject(rslt), Notice.class);
//		Account account=new Account();
//		
//		account.setUsername(user.getUsername());
//		account.setPassword(user.getPassword());
		
		this.setUsers(user);
		
        list = noticeDAO.findAll();
        this.setCommand("success123");
//		return "login2";
		return SUCCESS;
	}
	
	
	/*
	 * 流的形式返回
	 */
//	public String login(){
//		
//		System.out.println(user.getUsername()+":"+user.getPassword());
//		
//		Map<String, String> map = new HashMap<String, String>();  
//		
//        map.put("name", user.getUsername()); 
//        
//        map.put("password",user.getPassword());  
//        
//        list = stuManager.getList();
//        
//        
////        JSONObject obj = JSONObject.fromObject(map); 
////        result = obj.toString(); 
//        
//        JSONArray jsonarray = JSONArray.fromObject(list);
////        
////        result = jsonarray.toString(); 
//        
//        ActionContext  context = ActionContext.getContext();
////		HttpServletRequest  request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
//        
//		HttpServletResponse rspon = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
//		
//        System.out.println("result:"+result); 
//        
//        rspon.setCharacterEncoding("utf-8");  
//        rspon.setCharacterEncoding("utf-8");  
//        rspon.setContentType("text/html");
//        
//         PrintWriter out = null;  
//        try {  
//            out = rspon.getWriter();  
//        } catch (IOException e) {  
//              
//            e.printStackTrace();  
//        }    
//        
//        
//        out.print(jsonarray);  
//        
//        out.flush();  
//        out.close();  
//          
//		return SUCCESS;
//	}
	
	public String save(){
		
//		System.out.println("haha"+username);
		
	    System.out.println(user.getUsername()+":"+user.getPassword());
		
		list = stuManager.getList();
		return SUCCESS;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getHaha() {
		return haha;
	}

	public void setHaha(String haha) {
		this.haha = haha;
	}

	
	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	public Account getUsers() {
		return users;
	}

	public void setUsers(Account users) {
		this.users = users;
	}

	public Account getUser() {
		return user;
	}

	public void setUser(Account user) {
		this.user = user;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public void setStuManager(stuManager stuManager) {
		this.stuManager = stuManager;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}


	public Notice getNoticeBean() {
		return noticeBean;
	}


	public void setNoticeBean(Notice noticeBean) {
		this.noticeBean = noticeBean;
	}

		
	
	

}

 附上整个工程的源码,如有需要可以下载,由于时间原因,程序中难免有不足,忘大家指点出来,共同进步!thank you!

猜你喜欢

转载自taoge-tony.iteye.com/blog/1965179