`
TiFa.L.Hart
  • 浏览: 15981 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
上传(创建文件路径等)
public void upload(String savePaths) throws Exception{
		String path = ServletActionContext.getServletContext().getRealPath(savePaths);
		String fileName = this.getFileName();
		File dir = new File(path);
		if(!dir.exists()){
			dir.mkdir();
		}
		String outPath  = path+"\\"+fileName;
		FileOutputStream out = new FileOutputStream(outPath);
		FileInputStream in = new FileInputStream(this.getUpload());
		byte[] buffer= new byte[4069];
		int len = -1;
		while((len = in.read(buffer))>0){
			out.write(buffer, 0, len);
		}
		out.flush();
		in.close();
		out.close();
		this.uploadFileName = fileName;
		this.savePath = savePaths;
		ActionContext.getContext().getSession().put("fileName", fileName);
		ActionContext.getContext().getSession().put("fileType", this.getUploadContentType());
	    ActionContext.getContext().getSession().put("filePath", outPath);
	}
	
	public String getFileName() throws Exception{
		int index = this.getUploadFileName().lastIndexOf(".");
		String extension = this.getUploadFileName().substring(index);
		int randName = new Random().nextInt(100);
		return System.currentTimeMillis()+randName+extension;
	}
Spring配置---Bean(ApplicationContext) applicationcontext
<?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" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		   http://www.springframework.org/schema/context
		   http://www.springframework.org/schema/context/spring-context-2.5.xsd
		   http://www.springframework.org/schema/util
		   http://www.springframework.org/schema/util/spring-util-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
        

         <!-- jdbc方式获取数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/hibernate"/>
		<property name="username" value="root"/>
		<property name="password" value="637562588"/>
	</bean>


	<!-- Jndi方式获取数据源(能够实现连接池) -->
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="bm"></property>
		<property name="resourceRef" value="false"></property>
		<property name="jndiEnvironment">
			<props>
				<prop key="java.naming.provider.url">t3://localhost:7001</prop>
				<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
			</props>
		</property>
	</bean>

	<!-- 与Hibernate整合,配置sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.connection.release_mode">auto</prop>
				<prop key="hibernate.bytecode.user_reflection_optimizer">true</prop>
				<prop key="connection.provider_class">org.hibernate.connection.ProxoolConnectionPrvoider
				</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
				<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory
				</prop>
				<prop key="hibernate.jdbc.fetch_size">0</prop>
				<prop key="hibernate.jdbc.batch_size">0</prop>
				<prop key="hibernate.default_batch_size">0</prop>
				<prop key="hibernate.genernate_statistics">false</prop>
				<prop key="hibernate.connection.autocommit">false</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="javax.persistence.validation.mode">none</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/tifa/bookmark/beans/User.hbm.xml</value>
				<value>com/tifa/bookmark/beans/Bookmark.hbm.xml</value>
			</list>
		</property>
	</bean>
	<bean id="userDAO" class="com.tifa.bookmark.dao.UserDao">
		<property name="hibernateTemplate" ref="hibernageTemplate" />
	</bean>

	<bean id="bookmarkDAO" class="com.tifa.bookmark.dao.BookmarkDao">
		<property name="hibernateTemplate" ref="hibernageTemplate" />
	</bean>
	
	<!-- hibernate的事务管理类 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 事务模板类 -->
	<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
		<constructor-arg ref="transactionManager"/>
	</bean>
	
	<bean id="hibernageTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="userDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="proxyInterfaces">
			<list>
				<value>com.tifa.bookmark.Idao.IUserDao</value>
			</list>
		</property>
		<property name="target" ref="userDAO"></property>
		<property name="transactionManager" ref="transactionManager"/>
		<property name="transactionAttributes">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>

</beans>
Spring配置---MVC mvc-config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<!-- view(试图)层解析、即指定路径,以及试图文件的后缀,但是结果的试图文件的名称就要根据不同的Controller所返回的名称决定 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"/>
	</bean>
    
    <!-- 
    	当前台有请求传过来的时候,前端控制器就会在这里查找相对应的Controller类来处理(传参并返回ModelAndView)。
    	由于Spring MVC 默认是使用BeanNameUrlHandlerMapping这一个类来决定请求由哪个bean(也就是Controller类)。
    	如果是用这种方法的话,就不需要在当前文件中声明该BeanNameUrlHandlerMapping 的bean<若是使用这种方法,则
    	每个Controller的bean的name属性就等于各自请求的名称,如:处理名为/hello.action请求的Controller类的bean的name="/hello.action">
		,这种方法一般适合小程序,因此在这里就没有使用到这种方法。(除了BeanNameUrlHandlerMapping这种外,还有SimpleUrlHandlerMapping以及
		ControllerClassNameHandlerMapping。这里使用的是SimpleUrlHandlerMapping):
     -->      
     <bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     	<property name="mappgings">
     		<props>
     			<prop key="/course.action">courseController</prop>
     		</props>
     	</property>
     </bean>
     
     
     <!-- 一个Controller处理多个请求,不同请求不同的处理方法,因此以下的bean就是配置不同的请求不同的处理方法名称 -->
     <bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
     <!-- 根据表单传过来的type属性的值来决定不同的 方法来处理 -->
     	<property name="paramName" value="type"/>
     </bean>
     
     <!--
	 由于Controller里主要处理页面的请求(过于繁忙),因而Controller里不太适合有太多的业务逻辑处理的代码,因此可以为它配置一个delegate为它处理业务逻辑代码 
	<bean id="courseDelegate" class="com.tifa.delegate.CourseDelegate">
		<property name="viewPage" value="result"/>
	</bean>
     -->
     
     <!-- Controller -->
     <!--
     <bean id="courseController" class="com.tifa.controller.CourseController">
     	<property name="methodNameResolver" ref="paraMethodResolver"/>
     	<property name="delegate" ref="courseDelegate"/>
     </bean>  -->
     <bean id="register" class="com.tifa.bookmark.controller."
     
     
     </beans>
Spring配置---web.xml web.xml, spring
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="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>
  
  
  <!-- 解决乱码问题的过滤器 -->
  <filter>
  	<filter-name>encodingFilter</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>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--
  
   一下的配置是告诉WebApplicationContext,Spring的配置文件在哪里,若不指定名称,则默认为applicationContext.xml 
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>/WEB-INF/beans-config.xml</param-value>
  </context-param>
  
  -->
  <listener>
  	<listener-class>
  		org.springframework.web.context.ContextLoaderListener
  	</listener-class>
  </listener>
  
  
  <!-- 前端控制器 -->
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指定Controller的配置文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>/WEB-INF/mvc-config.xml</param-value>
  	</init-param>
  	<!-- 配置优先权为最大 -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
</web-app>
两个数互换 java http://softbeta.iteye.com/blog/1185766
                int x = 2010;   
		int y = 2012;   
		y=(x^= (y^= x))^ y;
		System.out.println("x= " + x + "; y= " + y);
Global site tag (gtag.js) - Google Analytics