Category Archives: Java

「Spring MVC開発」DispatcherServletの使い方

Javaコード:
public interface HandlerAdapter {
public boolean supports(Object handler);
public void handle(Object handler);
}

public interface Controller {

}

public class HttpController implements Controller{
public void doHttpHandler(){
System.out.println(“http…”);
}
}

public class SimpleController implements Controller{
public void doSimplerHandler(){
System.out.println(“simple…”);
}
}

public class AnnotationController implements Controller{
public void doAnnotationHandler(){
System.out.println(“annotation…”);
}
}

public class SimpleHandlerAdapter implements HandlerAdapter {
public void handle(Object handler) {
((SimpleController)handler).doSimplerHandler();
}

public boolean supports(Object handler) {
return (handler instanceof SimpleController);
}

}
public class HttpHandlerAdapter implements HandlerAdapter {

public void handle(Object handler) {
((HttpController)handler).doHttpHandler();
}

public boolean supports(Object handler) {
return (handler instanceof HttpController);
}

}
public class AnnotationHandlerAdapter implements HandlerAdapter {

public void handle(Object handler) {
((AnnotationController)handler).doAnnotationHandler();
}

public boolean supports(Object handler) {

return (handler instanceof AnnotationController);
}

}
//DispatcherServlet
import java.util.ArrayList;
import java.util.List;
public class DispatchServlet {

public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();

public DispatchServlet(){
handlerAdapters.add(new AnnotationHandlerAdapter());
handlerAdapters.add(new HttpHandlerAdapter());
handlerAdapters.add(new SimpleHandlerAdapter());
}
public void doDispatch(){

SimpleController controller = new SimpleController();
HandlerAdapter adapter = getHandler(controller);
adapter.handle(controller);

}

public HandlerAdapter getHandler(Controller controller){
for(HandlerAdapter adapter: this.handlerAdapters){
if(adapter.supports(controller)){
return adapter;
}
}
return null;
}

public static void main(String[] args){
new DispatchServlet().doDispatch();
}

}

「Spring MVC開発」静的リソースファイルを設定するコード

サンプルコード
<mvc:resources mapping=”/css/**” location=”/WEB-INF/resources/css/” />
<mvc:resources mapping=”/js/**” location=”/WEB-INF/resources/js/” />
<mvc:resources mapping=”/img/**” location=”/WEB-INF/resources/img/” />
<mvc:resources mapping=”/upload/**” location=”/WEB-INF/resources/upload/” />
<mvc:resources mapping=”/export/**” location=”/WEB-INF/resources/export/” />

「Java入門」バイト配列を16進数の文字列に変換するコード

Javaコード
public static String byte2hexFunc(byte[] bytes) {
final String strHex = “0123456789abcdef”;
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
sb.append(strHex.charAt((b >> 4) & 0x0f));
sb.append(strHex.charAt(b & 0x0f));
}

return sb.toString();
}

「org.apache.struts2.dispatcher.SessionMapにセッションを取得する方法

Javaコード
public V get(Object key) {
if(this.session == null) {
return null;

} else {
synchronized(this.session.getId().intern()) {
return this.session.getAttribute(key.toString());
}
}
}

「Apache Commons Lang」パッケージにhashCode()とequals()のコード

Javaコード
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public class Employee
{
private Integer id;
private String firstname;
private String lastName;
private String department;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public int hashCode()
{
final int PRIME = 31;
return new HashCodeBuilder(getId()%2==0?getId()+1:getId(), PRIME).
toHashCode();
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (o == this)
return true;
if (o.getClass() != getClass())
return false;
Employee e = (Employee) o;
return new EqualsBuilder().
append(getId(), e.getId()).
isEquals();
}
}

「Java入門」hashCode() を実装するサンプルコード

Javaコード
protected Integer emp_level = null;
@Override
public int hashCode() {
final int hashval = 31;
int flag =1;
flag = hashval * result + ((emp_level == null) ? 0 : emp_level.hashCode());
return flag;
}

「Java」文字列を置き換えるサンプルコード

Javaコード:
public static String strReplace(String deststr,String beforestr,String afterstr)
{
if(deststr == null || deststr.length()==0){
return “”;
}

if(beforestr == null || beforestr.length()==0){
return deststr;
}

return deststr.replace(beforestr,afterstr);
}

「Struts2開発」 ActionContextの使い方

1.ActionContextの取得
ActionContext cft= ActionContext.getContext();

2.リクエストパラメータの取得
Map<String,Object> parameters = cft.getParameters();

3.インスタンスの取得
ActionInvocation invoker = cft.getActionInvocation();

4.getValueStack()を使ってview情報を取得
ValueStack valueStack = cft.getValueStack();
Object value = valueStack.findValue(“dto.username”, true);

5.自動型変換エラーの取得
Map<String,Object> convertionErrors = cft.getConversionErrors();

6.HttpServletRequestの取得
final HttpServletRequest request = ServletActionContext.getRequest();

「struts2開発」リソースフォルダ(resources)のパスを取得するコード

サンプルコード:
import org.apache.struts2.ServletActionContext;
private final String getResourcePath(){
return ServletActionContext.getServletContext().getRealPath(“/WEB-INF/resources”);

}

「Java入門」FileUtilsクラスに forceDelete()でディレクトリを削除するコード

Javaコード
public static void forceDelete(File file) throws IOException {
if(file.isDirectory()) {
deleteDirectory(file);
} else {
boolean filePresent = file.exists();
if(!file.delete()) {
if(!filePresent) {
throw new FileNotFoundException(“File does not exist: ” + file);
}
String message = “Unable to delete file: ” + file;
throw new IOException(message);
} }
}