【动态更新】java小技巧随记

1.hibernate,update属性:

更新用户信息时,因为不含修改password,所以每次更新数据库时,都抛出password列不能为空的异常。

我们可以使用属性update=”false“,应用在property上,该列在update操作中将永远不会更新。

例子:<property name="password" column="password" update="false" />

还有一个属性dynamic-update=”true“,应用在class上,这样在更新该实例时将生成动态的update语句,如果更新时某属性值未发生变化,就不会加入到update语句中。

例子:<class name="com.opzoon.license.domain.User" table="t_user" dynamic-update="true">...</class>

但此属性用在这里不能实现我的目的。还在研究中。。。

注:hibernate中还有insert跟update类似,但dynamic-insert是忽略null值,将是null的属性不添加到insert语句中。

2.spring,@RequestBody:

使用@RequestBody时,要引入org.codehaus.jackson。否则不能转换json类型的参数,而且不会提示缺少该jar包。

另外,要将json转换bean时,http的Content-Type为application/json。

3.spring,@PostConstruct:

(1)tomcat启动时会调用标有该注解的方法。

(2)该方法的类上要有@Service,并且该方法为public。

4.java不常用到得关键字:

1.transient:标记为transient的变量,在对象存储时,这些变量状态不会被持久化。当对象序列化的保存在存储器上时,不希望有这些字段数据被保存,为了保证安全性,可以把这些字段声明为transient。

2.volatile:在每次被线程访问时,都强迫从共享内存中重读该成员变量的值。而且,当成员变量发生变化时,强迫线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。

5.jsp配置异常页面:

<error-page>
	<exception-type>xxx.xxx.xxx.xxx.Exception</exception-type>
	<location>/error.jsp</location>
</error-page>
error.jsp
<%@ page isErrorPage='true' %> 
<%=exception.getMessage()%>

6.spring.xml配置文件的命名空间:

在spring.xml配置文件中,有主要命名空间和普通命名空间之分。

主要命名空间的配置为xmlns="..."。配置之后,下面的配置文件中用到该命名空间,都可以省略。

普通命名空间的配置为xmlns:param="..."。配置之后,配置文件中用到该命名空间时不可省略,使用方式为<param:tag>...</param:tag>。其中,param可任意起名。

例如:主要命名空间为beans的话:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:beans="http://www.springframework.org/schema/security"
	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-3.0.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <security:http>
    	...
    </security:http>

</beans>
主要命名空间为security的话:

<beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:beans="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-3.0.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <http>
    	...
    </http>

</beans:beans>
7.maven项目打包时,不打包*.hbm.xml文件:

解决方法:

<build>
    <finalName>xxx</finalName>
    <resources>  
      <resource>  
          <directory>src/main/java</directory>  
          <includes>  
              <include>**/*.hbm.xml</include>  
          </includes>  
      </resource>  
      <resource>  
          <directory>src/main/resources</directory>  
          <includes>  
              <include>**/*.xml</include>  
              <include>**/*.properties</include>  
          </includes>  
      </resource>  
  </resources>  
</build>
8.CDATA:

xml文件中,用CDATA可以放入非法的字符:

<![CDATA[

]]>

9.Spring读取.properties文件的方法:

<!-- 1 -->
<bean id="propertyConfiger" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="location" value="database.properties"></property>
</bean>
<!-- 2 -->
<context:property-placeholder location="classpath:database.properties" />
<!-- 3 -->
<bean id="propertyConfiger" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
     <property name="location" value="database.properties"></property>
</bean>
<!-- 4 -->
<context:property-override location="classpath:database.properties" />

以上的方式可以用以下方法读取配置的属性:

<property name="username" value="${username}" />
还有一种方式如下:
<!-- 5 -->
<util:properties id="database" location="classpath:database.properties" />
读取方式也与上面不同:

<property name="username" value="#{database['username']}" />

10.servlet接收js传递的数组:

String[] names = request.getParameterValues("names[]");

[]表示的是接收的是一个数组,必须写。如果不写,则接收到的只有一个元素,是所有数组连接成的字符串。

11.java中的正则表达式:

在正则表达式中,\表示的是转义字符,如果要匹配真正的字符串'\',需要将其转义,用\\来匹配字符串"\"。

而在java中,\也是转义字符,所以字符串中的\要写成"\\"。

所以,正则表达式中的\\表示\,java字符串中的\\表示\。以此类推,java字符串表示的正则表达式中\\\\表示\。

例如:我要把字符串中的单斜线替换成双斜线:

str.replaceAll("\\\\", "\\\\\\\\");

12.tomcat支持带中文的url:

在tomcat中的server.xml中的Connector中添加两个设置:
useBodyEncodingForURI="true" //设置POST和GET使用相同编码
URIEncoding="UTF-8" //对URI使用utf-8编码处理

<Connector useBodyEncodingForURI="true" URIEncoding="UTF-8" connectionTimeout="20000" maxThreads="150" port="8888" protocol="HTTP/1.1" redirectPort="8443"/>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值