หน้าเว็บ

แสดงบทความที่มีป้ายกำกับ spring mvc แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ spring mvc แสดงบทความทั้งหมด

วันพฤหัสบดีที่ 18 เมษายน พ.ศ. 2556

image resize java

PhotoUtils.java
package com.blogspot.na5cent.utils;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

/**
 *
 * @author redcrow
 */
public class PhotoUtils {

    private static final int DEFAULT_TYPE = 5;

    public static BufferedImage resize(BufferedImage image, int newWidth, int newHeight) {
        int oldWidth = image.getWidth();
        int oldHeight = image.getHeight();

        if (oldHeight < newHeight || oldWidth < newWidth) {
            newHeight = oldHeight;
            newWidth = oldWidth;
        }else{
            float percentWidth = newWidth / (float) oldWidth;
            float percentHeight = newHeight / (float) oldHeight;

            if (newWidth == 0) {
                newWidth = (int) (percentHeight * oldWidth);
            }

            if (newHeight == 0) {
                newHeight = (int) (percentWidth * oldHeight);
            }
        }

        BufferedImage resized = new BufferedImage(newWidth, newHeight, DEFAULT_TYPE);
        Graphics2D g = resized.createGraphics();
        g.drawImage(image, 0, 0, newWidth, newHeight, null);
        g.dispose();

        return resized;
    }
}
Example to use 

วันพฤหัสบดีที่ 11 เมษายน พ.ศ. 2556

set session locale ผ่าน controller spring mvc : java

dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:p="http://www.springframework.org/schema/p"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    
    <context:component-scan base-package="com.blogspot.na5cent" />
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <mvc:resources mapping="/static/**" location="/resources/"/> 
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/web/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    
    
    <!-- internationalization locale -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
          p:basenames="languages/lang" />

    <!-- Declare the Interceptor -->
    <mvc:interceptors>    
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language" />
    </mvc:interceptors>

    <!-- Declare the Resolver -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="en"/>
    <!-- internationalization locale -->
</beans>
set default locale
controller
@Controller
public class FirstController {

    private static final Locale THAI_LOCALE = new Locale("th", "TH");
    @Autowired
    private SessionLocaleResolver sessionLocaleResolver;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView getIndex() {
        ModelAndView mav = new ModelAndView("index");

        sessionLocaleResolver.setDefaultLocale(THAI_LOCALE); //****
        return mav;
    }
}
set dynamic locale
controller
@Controller
public class LocaleController implements Serializable {

    private static final Locale THAI_LOCALE = new Locale("th", "TH");
    private static final Logger LOG = LoggerFactory.getLogger(LocaleController.class);
    @Autowired
    private SessionLocaleResolver sessionLocaleResolver;

    @RequestMapping(value = "/language/{keyCode}", method = RequestMethod.GET)
    @ResponseBody
    public void getIndex(@PathVariable(value = "keyCode") String keyCode, HttpServletRequest request, HttpServletResponse response) {

        Locale locale;
        if (keyCode.equals("en")) {
            locale = Locale.ENGLISH;
        } else {
            locale = THAI_LOCALE;
        }
        
        LOG.debug("keyCode => {}", keyCode);
        LOG.debug("locale => {}", locale);

        sessionLocaleResolver.setLocale(request, response, locale); //***
    }
}

วันเสาร์ที่ 22 กันยายน พ.ศ. 2555