MVC1.0とは?

最初に普通の JavaEE7 Web アプリを作る

mvc3.png

MVC1.0 関連のライブラリ (Ozark, Thymeleaf) を追加

https://github.com/kagyuu/MVC10Exam/blob/master/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>MVCExam</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>MVCExam</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.7</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.ozark</groupId>
            <artifactId>ozark</artifactId>
            <version>1.0.0-m02</version>
        </dependency>
        <!--
        This ext library depends on the thymeleaf 2, not 3.
        In the thymeleaf 2, the template must be XHTML. But in the thymeleaf 3,
        the template is allowed free style HTML. So, I write ozark extension 
        for thymeleaf3, currently.
        <dependency>
            <groupId>org.glassfish.ozark.ext</groupId>
            <artifactId>ozark-thymeleaf</artifactId>
            <version>1.0.0-m02</version>
        </dependency>
        -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.6.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Ozark のテンプレートエンジンとして Thymeleaf3 を使う

Thymeleafの機能を使って、i18n

https://github.com/kagyuu/MVC10Exam/blob/master/src/main/java/com/example/mvcexam/ext/ozark/MessageResolver.java

/*
 * An example for the MVC1.0m2 on the Glassfish 4.1.2 JEE7
 */
package com.example.mvcexam.ext.ozark;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import lombok.extern.slf4j.Slf4j;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.messageresolver.AbstractMessageResolver;
import org.thymeleaf.util.Validate;

/**
 * Process placeholders on Thymeleaf3 templates.
 * @author hondou.atsushi
 */
@Slf4j
public class MessageResolver extends AbstractMessageResolver {

    @Override
    public String resolveMessage(ITemplateContext context, Class<?> origin, String key, Object[] messageParameters) {
        ResourceBundle.Control control = 
            ResourceBundle.Control.getNoFallbackControl(
                    ResourceBundle.Control.FORMAT_DEFAULT);
        
        Locale locale = context.getLocale();
        log.debug("Message Locale={}", locale.toString());
        ResourceBundle bundle = ResourceBundle.getBundle("msg", locale, control);
        String message = bundle.getString(key);
        
        if (null == messageParameters || 0 == messageParameters.length) {
            return message;
        }
        
        final MessageFormat messageFormat = new MessageFormat(message, locale);
        return messageFormat.format(messageParameters);        
    }

    @Override
    public String createAbsentMessageRepresentation(ITemplateContext context, Class<?> origin, String key, Object[] messageParameters) {
        Validate.notNull(key, "Message key cannot be null");
        if (context.getLocale() != null) {
            return "??"+key+"_" + context.getLocale().toString() + "??";
        }
        return "??"+key+"_" + "??";
    }
    
}

CDIの機能を使って前処理・後処理

https://github.com/kagyuu/MVC10Exam/blob/master/src/main/java/com/example/mvcexam/ext/ozark/CommonEventObservver.java

/*
 * An example for the MVC1.0m2 on the Glassfish 4.1.2 JEE7
 */
package com.example.mvcexam.ext.ozark;

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.mvc.event.AfterControllerEvent;
import javax.mvc.event.AfterProcessViewEvent;
import javax.mvc.event.BeforeControllerEvent;
import javax.mvc.event.BeforeProcessViewEvent;
import javax.mvc.event.ControllerRedirectEvent;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

/**
 * MVC1.0の共通処理サンプル.
 * <pre>
 * コントローラ、Viewエンジンの開始終了に何らかの処理をはさむことができます。
 * ただし Model を操作することはできません。
 * </pre>
 * @author hondou.atsushi
 */
@Slf4j
@ApplicationScoped
public class CommonEventObservver {

    @Inject
    private HttpServletRequest req;

    public void onAfterControllerEvent(@Observes AfterControllerEvent event) {
        log.debug("AfterControllerEvent");

        req.setAttribute("__USER_NAME", req.getRemoteUser());

        try {
            req.setAttribute("__HOST_NAME", InetAddress.getLocalHost().getHostName());
        } catch (UnknownHostException e) {
            req.setAttribute("__HOST_NAME", "###");
        }
    }

    public void onAfterProcessViewEvent(@Observes AfterProcessViewEvent event) {
        log.debug("AfterProcessViewEvent");       
    }

    public void onBeforeControllerEvent(@Observes BeforeControllerEvent event) {
        log.debug("BeforeControllerEvent");      
    }

    public void onBeforeProcessViewEvent(@Observes BeforeProcessViewEvent event) {
        log.debug("BeforeProcessViewEvent");
    }

    public void onControllerRedirectEvent(@Observes ControllerRedirectEvent event) {
        log.debug("ControllerRedirectEvent");        
    }

}

Hello MVC1.0

MVC1.0 でも JAX-RS を使った Bean Validation

MVC1.0 の機能を使って CSRF 対策

Thymeleaf3 を使うもう一つの利点

参考文献


Java#Glassfish


添付ファイル: filehello3.png 25件 [詳細] filehello2.png 21件 [詳細] filehello1.png 24件 [詳細] filemsg.png 28件 [詳細] filecsrf.png 31件 [詳細] filevalidate4.png 29件 [詳細] filevalidate3.png 28件 [詳細] filevalidate2.png 27件 [詳細] filevalidate1.png 29件 [詳細] filemvc3.png 31件 [詳細] filemvc2.png 30件 [詳細] filemvc1.png 40件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2017-07-17 (月) 17:20:51 (98d)
ISBN10
ISBN13
9784061426061