Spring 2.5 servlet mapping - 다른예

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">

 <!-- Enables clean URLs with JSP views e.g. /welcome instead of /app/welcome -->
 <filter>
  <filter-name>UrlRewriteFilter</filter-name>
  <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>UrlRewriteFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
  
 <!-- Handles all requests into the application -->
 <servlet>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/spring/*.xml
   </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
  
 <!-- Maps all /app requests to the DispatcherServlet for handling -->
 <servlet-mapping>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <url-pattern>/app/*</url-pattern>
 </servlet-mapping>
 
</web-app>
-----------------------------------------------------------------------------
mvc-config.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:context="http://www.springframework.org/schema/context"
 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">

 <!-- HANDLER MAPPING RULES -->
 
 <!-- Maps requests to @Controllers based on @RequestMapping("path") annotation values
   If no annotation-based path mapping is found, Spring MVC proceeds to the next HandlerMapping (order=2 below). -->
 <!-- 실제로 이 예제에선 사용되지 않는다. -->
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  <property name="order" value="1" />
 </bean>
 
 <!-- Maps requests to @Controllers based on controller class name convention; e.g. a request for /hotels or a /hotels sub-resource maps to HotelsController
      If no class mapping is found, Spring MVC sends a 404 response and logs a pageNotFound warning. -->
    <!-- 예제는 이것을 사용 -->     
 <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
  <property name="order" value="2" />
 </bean>

 <!-- REGISTERED HANDLER TYPES -->

 <!-- Enables annotated @Controllers; responsible for invoking an annotated POJO @Controller when one is mapped. -->
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

 <!--  VIEW RESOLUTION AND RENDERING -->
 
 <!-- Resolves view names to protected .jsp resources within the /WEB-INF directory -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/pages/"/>
  <property name="suffix" value=".jsp"/>
 </bean>
 
</beans>
-----------------------------------------------------------------------------
app-config.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:context="http://www.springframework.org/schema/context"
 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">
 
 <!-- Scans within the base package of the application for @Components to configure as beans -->
 <!-- base-pacakage 의 하위 패키지에서 어노테이션을 사용한 클래스를 자동으로 등록한다
      따라서 <bean id=".." class=".."/>를 설정할 필요가 없다.
  -->
 <context:component-scan base-package="com.zoo.bookstore"/>
 
</beans>

by CHillo | 2009/10/28 15:40 | 트랙백 | 덧글(0)
Grails URL Mapping
Grails는 Controller에 package를 지정하여 사용할 수 없는 단점이 있다.
따라서 RESTFul 타입의 개발이나 기능별 모듈화가 필요할 경우에는
URL 매핑을 사용하여 해결하여야한다.
(사용예는 아래의 링크를 참조)

http://grails.org/URL+mapping
by CHillo | 2009/10/28 15:29 | 트랙백 | 덧글(0)
Spring 2.5 servlet mapping
dispatcher-servlet.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">

    <!-- ControllerClassNameHandlerMapping 는 요청 url의 앞부분이 pathPrefix에 해당한다면 basePackage 내의 클래스중 url에 해당하는 controller를 실행한다. 
    예를 들어 요청이 /sample/demo.html일 경우 com.springrest.web.controller.demo 패키지의 DemoController를 수행한다. -->
    <bean id="demoMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="pathPrefix" value="sample"/>
        <property name="basePackage" value="com.springrest.web.controller.demo"/>
    </bean>

    <bean id="adminMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="pathPrefix" value="admin"/>
        <property name="basePackage" value="com.springrest.web.controller.user"/>
    </bean>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.html">indexController</prop>
            </props>
        </property>
    </bean>
    
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
    
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />


    <!-- ControllerClassNameHandlerMapping 의 영향을 받는다 -->
    <bean id="demoController" class="com.springrest.web.controller.demo.DemoController"/>


    <!-- MultiActionController 사용 예 ../user/list.html -->
    <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver"/>

    <bean id="userController" class="com.springrest.web.controller.user.UserController">
        <!-- 기본적으로 프로퍼티명에 해당하는 bean id가 있을경우 자동으로 바인딩된다 -->
        <!--property name="methodNameResolver" ref="methodNameResolver"/-->
    </bean>

    <!-- 요청url에 따라 자동으로 view name을 찾는다. 이 경우 ../user/add.html 의 경우 /WEB-INF/jsp/admin/user/add.jsp 로 이동 -->
    <bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>
</beans>

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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">
    
    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />
    
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}" /-->
    
    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->


</beans>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

by CHillo | 2009/10/27 01:46 | 트랙백 | 덧글(0)
RoR, Grails, Zero, Spring Roo...
RoR 
  - 검증된 프레임워크, 가볍고 빠르며 개발이 용이하다
  - 훌륭한 IDE 제공 : Aptana (Eclipse기반) http://www.radrails.org/


Grails
  - 꾸준한 발전, Java기반 Rad프레임워크중 현재 가장 유용하다.
  - groovy, gsp등 스크립트 언어의 사용으로 개발이 빠르다.
  - Rails와 사용법이 비슷하다.
  - controller나 page에 package/sub-folder를 사용할 수 없어 모듈화된 대형프로그램 개발에는 부적합.
  - IDE는 보통 : 
    STS Integration (Eclipse기반) http://www.grails.org/STS+Integration
    NetBeans Integration http://www.grails.org/NetBeans+Integration


Roo
  - Grails의 단점을 개선하였다 - 패키지 사용가능, 사용이 용이한 자체콘솔, maven 통합.
  - Spring의 대부분의 모듈이 통합되어있다 (Spring-Security등)
  - 아직 rc버전이다.


Zero (sMesh)
  - 가장빠른 개발 속도, 훌륭한 아키텍처 및 지원 - RESTFul service 등.
  - groovy, php 등 다중 스크립트 언어 사용가능
  - 완전한 오픈소스가 아니다
  - 웹기반 통합 IDE 제공 



 



by CHillo | 2009/10/26 14:08 | 트랙백 | 덧글(0)
Hibernate, Log4J 설정 예
Hibernate

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--
        <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="hibernate.connection.url">jdbc:derby://localhost:1527/pm6</property>
        <property name="hibernate.connection.username">app</property>
        <property name="hibernate.connection.password">app</property>
        -->
        <property name="hibernate.session_factory_name">hibernate/SessionFactory</property>
        <property name="hibernate.connection.datasource">java:comp/env/PM6-DS</property>
        <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

        <property name="hibernate.transaction.factory_class">
            org.hibernate.transaction.CMTTransactionFactory        
        </property>
        <property name="hibernate.transaction.manager_lookup_class">
            org.hibernate.transaction.SunONETransactionManagerLookup
        </property>
        <property name="jta.UserTransaction">java:comp/UserTransaction</property>
        <property name="hibernate.current_session_context_class">jta</property>
        <property name="hibernate.transaction.auto_close_session">true</property>
        <property name="hibernate.transaction.flush_before_completion">true</property>
    </session-factory>

    ....

</hibernate-configuration>


Log4J

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, stdout

log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug

### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL
#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache=debug

### log transaction activity
#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace

by CHillo | 2009/10/25 07:16 | 트랙백 | 덧글(0)
EJB에서 Hibernate 사용시 주의사항.
1. SessionFactory는 JNDI를 통해 활용한다.

hibernate.cfg 파일의 session-factory 태그에 name 속성이 있을 경우 
자동으로 JNDI에 등록된다.

<session-factory name="java:hibernate/SessionFactory">

이후 JNDI 룩업을 통해 활용한다. (HibernateUtil 클래스를 생성하면 편리함)

...
Context ctx = new InitialContext();
SessionFactory sf =  (SessionFactory) ctx.lookup("java:hibernate/SessionFactory");
Session ss = sf.getCurrentSession();
...


2. Session은 SessionFactory.getCurrentSession()으로 얻는다.

openCurrentSession() 이나 commit(), close()를 사용하지 않고 컨테이너에서 관리하도록 한다.
by CHillo | 2009/10/23 21:54 | 트랙백 | 덧글(0)
Hibernate 설정 관련 FAQ

Configuration

Whats the easiest way to configure Hibernate in a plain Java application (without using JNDI)?

Build a SessionFactory from a Configuration object. See the tutorials in the reference documentation.

Whats the easiest way to configure Hibernate in a J2EE application (using JNDI)?

Build a SessionFactory from a Configuration object. See the tutorials in the reference documentation. Specify a name for the SessionFactory in the configuration file, so it will be bound automatically to this name in JNDI.

How do I configure Hibernate as a JMX service in JBoss

See Using Hibernate with JBoss.

How do I use Hibernate in an EJB 2.1 session bean?

1. Look up the SessionFactory in JNDI.

2. Call getCurrentSession() to get a Session for the current transaction.

3. Do your work.

4. Don't commit or close anything, let the container manage the transaction.

How do I configure logging?

Hibernate uses the Apache commons-logging abstraction layer to support whichever logging framework you hapen to be using. See

http://jakarta.apache.org/commons/logging.html

To configure log4j you need to do two things:

1. put log4j.jar in your classpath

2. put log4j.properties in your classpath

There is an example log4j.properties in the hibernate-x.x directory. Just change INFO to DEBUG to see more messages (and move it into the classpath).

To use JDK1.4 logging, do three things:

1. remove log4j.jar from the classpath

2. run under JDK1.4 ;)

3. configure logging via the properties file specified by the java.util.logging.config.file system property (this property defaults to $JAVA_HOME/jre/lib/logging.properties)

How do I configure the cache?

See the Performance Tuning chapter in the reference documentation.

by CHillo | 2009/10/23 21:15 | 트랙백 | 덧글(0)
< 이전페이지 다음페이지 >