DBHelper.java
package DAO;
import java.util.ArrayList;
import java.util.List;
import model.Article;
public class DBHelper {
public List<Article> selectAll(){
System.out.println("db에서 글 모두 가져온다");
List<Article> list=new ArrayList<Article>();
Article article1=new Article(1, "첫", "안녕1");
Article article2=new Article(2, "첫~", "안녕2");
Article article3=new Article(3, "첫~~", "안녕3");
Article article4=new Article(4, "첫~~~", "안녕4");
Article article5=new Article(5, "첫@@@", "안녕5");
list.add(article1);
list.add(article2);
list.add(article3);
list.add(article4);
list.add(article5);
return list;
}
}
Article.java
package model;
//게시글 하나의 정보를 담는 모델
public class Article {
private int no; //글번호
private String title; //제목
private String content; //내용
public Article(int no, String title, String content) {
super();
this.no = no;
this.title = title;
this.content = content;
}
public Article() {
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
ListService.java
package model;
//게시글 하나의 정보를 담는 모델
public class Article {
private int no; //글번호
private String title; //제목
private String content; //내용
public Article(int no, String title, String content) {
super();
this.no = no;
this.title = title;
this.content = content;
}
public Article() {
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
게시글 리스트를 보여주는 페이지 <br/>
<table>
</table>
<s:iterator value="list"> <!-- List<Article> list;// 게시글이 5개 들어갈 것이다. -->
<s:property value="no" /> /
<s:property value="title" /> /
<s:property value="content" />
</s:iterator>
<s:if test="true">
true면 항상 나옵니다 <br/>
</s:if>
<s:if test="1<5">
1<5 ? <br/>
</s:if>
<s:if test="false">
if
</s:if>
<s:elseif test="2==3">
elseif
</s:elseif>
<s:else>
if, elseif, else
</s:else> <br />
append는 2개의 리스트를 합칩니다.
<s:append id="totallist">
<s:param value="list" />
<s:param value="list" />
</s:append>
iterator는 list를 풀어서 반복시킵니다.
<s:iterator value="totallist">
<s:property value="no"/> , <s:property value="title"/>
, <s:property value="content" /> <br/>
</s:iterator>
generator는 iterator를 만듭니다. <br />
<s:generator id="list" separator="," val="%{ ' 안,녕,하,세,요' }"
count="5" >
<s:iterator>
<s:property />
</s:iterator>
</s:generator>
<br/> merge는 list를 합칠때 섞습니다
<s:merge id="totallist">
<s:param value="list" />
<s:param value="list" />
</s:merge>
<s:iterator value="totallist">
<s:property value="no"/> , <s:property value="title"/>
, <s:property value="content" /> <br/>
</s:iterator>
bean은 클래스의 객체를 만들때 사용합니다 <br/>
<s:bean name="model.Article" id="art">
<s:param name="no" value="7" />
<s:param name="title" value="'타이틀'" />
<s:param name="content" value="'~~컨텐트~~'" />
</s:bean> <br />
no: <s:property value="#art.no" />
title : <s:property value="#art.title" />
content : <s:property value="#art.content" />
set은 특정 영역에 값을 셋팅합니다. 예) 로그인 유무 <br />
<s:set name="title" value="#art.title" scope="application"></s:set>
<s:set name="ses" value="'세션영역에 값 셋팅'" scope="session" />
<s:set name="req" value="'리퀘스트 영역에 값 셋팅'" scope="request" />
<s:set name="pag" value="'페이지 영역에 값 셋팅'" scope="page" />
<s:set name="hello" value="#art.no" />
특정 영역에 셋팅된 값을 꺼내기 <br />
app : <s:property value="#application.title" /> , ${title} <br/>
ses : <s:property value="#session.ses" /> , ${ses } <br />
pag : <s:property value="#page.pag" /> , ${pag} <br/>
hello : <s:property value="#hello"/> , ${hello } <br/>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="hi" extends="struts-default"
namespace="/" >
<action name="list" class="Service.ListService" >
<result name="success">/list.jsp</result>
</action>
<!-- 맨 아래에 있어야 합니다.
디폴트 와일드카드 맵핑
-->
<action name="*">
<result>{1}.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>04.validate</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>