Archive for Java Appication

Spring Internationalization Setting

  1. in jdbc.properties:
    jdbc.url=jdbc:mysql://localhost:xxxx/yyyy?autoReconnect=true&characterEncoding=UTF-8
  2. in /WEB-INF/web.xml:
    <filter>
        <filter-name>encoding-filter</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>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
     <bean id="secureHandlerMapping" 
         class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
        <property name="interceptors">
          <list>
            <ref bean="loginInterceptor"/>
            <ref bean="localeChangeInterceptor" />
          </list>
        </property>
      </bean>
  3. In /WEB-INF/spring/spring-servlet.xml
     <bean id="messageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8"/>
      </bean>
    
      <bean id="localeResolver">
          <property name="defaultLocale" value="en"/>
      </bean>
      <bean id="localeChangeInterceptor">
          <property name="paramName" value="lang" />
      </bean>
  4. In jsp file
    <%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
  5. Language links in jsp file:
    <li><a href=”?lang=cn”><spring:message code=”index.chinese”/></a></li>
    <li><a href=”?lang=en”>English</a></li>
  6. In class folder:
    messages_en.properties
    messages_cn.properties
  7. In messages_en.properties
    index.schoolName=School Name
  8. In messages_cn.properties
    index.schoolName=\u5b66\u6821\u540d\u79f0
  9. Create utf-8 code in messages_cn.properties
  • copy messages_en.properties to messages_cn.properties
  • translate in messages_cn.properties
  • use tool native2ascii.exe
    C:\……\jdk\bin\native2ascii.exe    -encoding utf8        C:\fromFolder\messages_cn.properties  C:\toFolder\messages_cn.properties

		
Advertisements

Leave a Comment

How to disable browser back after logout

For jsp, put the following code on every page:

<%
response.setHeader(“Cache-Control”,”no-cache”); //HTTP 1.1
response.setHeader(“Cache-Control”,”no-store”); //HTTP 1.1
response.setHeader(“Pragma”,”no-cache”); //HTTP 1.0
response.setDateHeader (“Expires”, 0); //prevents caching at the proxy server
%>

Leave a Comment

Basic Spring Project Setup Template

Download(new)
Download

Modules:

  • user signup/login/edit module
  • session management module
  • security interceptor module
  • global code list
  • user management module including user list, delete,active/inactive…
  • code type/code management module including list, delete, reorder…

How to use it

assume local folder is “c:\WYX\tmp”

  1. Extract zip file to your local “c:\WYX\tmp” and change the folder name from “yaan” to “myfirstproject”.
  2. New project in IDE, e.g. IntelliJ IDEA:
    project name:      “myfirstproject”,
    project location: c:\WYX\tmp\myfirstproject,
    Create module:    checked
    module name:     java
    module content root:   c:\WYX\tmp\myfirstproject\src\java
    module filelocation:     c:\WYX\tmp\myfirstproject\
  3. new module
    module name:     web
    module content root:   c:\WYX\tmp\myfirstproject\src\web
    module filelocation: c:\WYX\tmp\myfirstproject\
    next:   Do not create source directory
  4. In “Project Structure”, create “myfirstproject_lib” from lib folder
  5. Global replace “greenbestow” to “myfirstproject”, “GreenBestow” to “Myfirstproject”
  6. Change package name “greenBestow” to “myfirstproject”
  7. Change jdbc.properties
  8. In build.xml cahnge home, libhome, war_file
  9. Change greenBestow-servlet.xml to myfirstproject-servlet.xml
  10. change BaseJdbcService.java:
    public class BaseJdbcService extends NamedParameterJdbcDaoSupport {
    }
  11. for insert query, using:
    SqlParameterSource fileParameters = new BeanPropertySqlParameterSource(tag);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    int row = getNamedParameterJdbcTemplate().update(q, fileParameters, keyHolder);
    if (row>0) {
    tag.setId(keyHolder.getKey().intValue());
    }
  12. add the following into web.xml
    <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>[what you have in display-name or something unique to your web app]</param-value>
    </context-param>

Download(new)
Download

Leave a Comment

Remote debug setting

set JAVA_OPTIONS=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n

in IntelliJ IDEA:

  1. add new Configurations – Remote
  2. change the port to 8001

Leave a Comment

SEVERE: Error listenerStart

When you start tomcat server and get the error “SEVER: Error listenerStart”… “SEVERE: Context [/xxxxxxxxxx] startup failed due to previous errors”, it is because you have multiple apps under webapp/ folder. You need add the following lines in every app’s web.xml

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>xxxxxxxxxx.root</param-value>
</context-param>

It is Log4jWebConfigurer’s requirement…

http://opensource.objectsbydesign.com/spring-1.1.4/org/springframework/web/util/Log4jWebConfigurer.html

Leave a Comment

How to call a javascript function in the iframe?

document.getElementById(‘frame_id’).contentWindow.function_name()

Leave a Comment

Page not found error in a jsp

1) Put the following code in web.xml:

<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error/error.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/error/http400.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error/http404.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/error/http403.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/http500.jsp</location>
</error-page>

2) Create above jsp files. Usually to be safe you should make error pages that are larger then 512 bytes. The threshold varies per HTTP status code. You can look at what your thresholds are currently set to. In IE 5 and greater the settings are stored in the registry under[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\ErrorThresholds]

Err Size(bytes)
400 512
403 256
404 512
405 256
406 512
408 512
409 512
410 256
500 512
501 512
505 512

Leave a Comment

Older Posts »