|
解凍するファイル
C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all.zip 解凍するとできるフォルダ C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0 |
|
解凍するファイル
C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0\apps\struts2-rest-showcase-7.0.0.war 解凍するとできるフォルダ C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0\apps\struts2-rest-showcase-7.0.0 |
|
Server: localhost (デフォルト)
Database: postgres (デフォルト) Port: 5432 (デフォルト) Username: postgres (デフォルト) Client Encoding: SJIS (デフォルト) ユーザ postgres のパスワード: (自分で設定したパスワード) |
|
-- ユーザ作成
CREATE ROLE testuser WITH LOGIN NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS PASSWORD 'userpass'; -- Database作成 CREATE DATABASE mydb WITH OWNER = testuser ENCODING = 'UTF8' LC_COLLATE = 'C' LC_CTYPE = 'C' LOCALE_PROVIDER = 'libc' TABLESPACE = pg_default CONNECTION LIMIT = -1 IS_TEMPLATE = False; |
|
\c mydb testuser |
|
-- スキーマ作成
CREATE SCHEMA IF NOT EXISTS myschema AUTHORIZATION testuser; -- テーブル作成 CREATE TABLE myschema.weather ( city varchar(80), -- charcter varying型 temp_lo int, -- 最低気温 integer型 temp_hi int, -- 最高気温 integer型 prcp real, -- 降水量 real型 date date -- date型 ); -- データの作成 INSERT INTO myschema.weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27'); INSERT INTO myschema.weather (city, temp_lo, temp_hi, prcp, date) VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29'); INSERT INTO myschema.weather (date, city, temp_hi, temp_lo) VALUES ('1994-11-29', 'Hayward', 54, 37); |
|
コピー元
C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0\apps\struts2-rest-showcase-7.0.0\WEB-INF\lib\* コピー先 C:\pleiades\2024-12\workspace\(プロジェクト名)\src\main\webapp\WEB-INF\lib (重要)コピー先から以下のjarファイルを削除 struts2-rest-plugin-7.0.0.jar |
|
コピー元
C:\Program Files (x86)\PostgreSQL\pgJDBC コピー先 C:\pleiades\2024-12\workspace\(プロジェクト名)\src\main\webapp\WEB-INF\lib |
|
コピー元
C:\Users\(ユーザ名)\Downloads\commons-dbcp2-2.13.0-bin\commons-dbcp2-2.13.0 コピー先 C:\pleiades\2024-12\workspace\(プロジェクト名)\src\main\webapp\WEB-INF\lib |
|
package test7.sample.model; import java.math.BigDecimal; public class Weather { private String city; private Integer temp_lo; private Integer temp_hi; private BigDecimal prcp; private String date; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Integer getTemp_lo() { return temp_lo; } public void setTemp_lo(Integer temp_lo) { this.temp_lo = temp_lo; } public Integer getTemp_hi() { return temp_hi; } public void setTemp_hi(Integer temp_hi) { this.temp_hi = temp_hi; } public BigDecimal getPrcp() { return prcp; } public void setPrcp(BigDecimal prcp) { this.prcp = prcp; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } } |
|
package test6.sample.dao; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; public class InitDataSource { private static Context context = null; private static DataSource ds = null; public static DataSource getInstance() throws Exception { if (context == null) { context = new InitialContext(); } if (ds == null) { ds = (DataSource)context.lookup("java:comp/env/mydb"); } return ds; } } |
|
package test7.sample.dao; import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import test7.sample.model.Weather; public class WeatherDAO { DataSource ds; public WeatherDAO() throws Exception { ds = InitDataSource.getInstance(); } public List<Object> selectWeather() throws Exception { PreparedStatement ps = null; ResultSet rs = null; List<Object> weatherList = new ArrayList<>(); Connection conn = ds.getConnection(); conn.setSchema("myschema"); // String SQL = "SELECT CITY, TEMP_LO, TEMP_HI, PRCP, DATE FROM MYSCHEMA.WEATHER"; String SQL = "SELECT CITY, TEMP_LO, TEMP_HI, PRCP, DATE FROM WEATHER"; try { ps = conn.prepareStatement(SQL); rs = ps.executeQuery(); while(rs.next()){ String city = rs.getString("city"); Integer temp_lo = rs.getInt("temp_lo"); Integer temp_hi = rs.getInt("temp_hi"); BigDecimal prcp = rs.getBigDecimal("prcp"); String date = rs.getDate("date").toString(); Weather weather = new Weather(); weather.setCity(city); weather.setTemp_lo(temp_lo); weather.setTemp_hi(temp_hi); weather.setPrcp(prcp); weather.setDate(date); weatherList.add(weather); } } finally { ps.close(); rs.close(); conn.close(); } return weatherList; } } |
|
package test7.sample.action; import java.util.ArrayList; import java.util.List; import org.apache.struts2.ActionSupport; import test7.sample.dao.WeatherDAO; public class WeatherListAction extends ActionSupport { private static final long serialVersionUID = 1L; private List<Object> weatherList = new ArrayList<>(); public String execute() throws Exception { WeatherDAO dao = new WeatherDAO(); weatherList = dao.selectWeather(); return SUCCESS; } public List<Object> getWeatherList() { return weatherList; } public void setWeatherList(List<Object> weatherList) { this.weatherList = weatherList; } } |
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Weather List</title> </head> <body> <table> <tr> <td>city</td> <td>temp_lo</td> <td>temp_hi</td> <td>prcp</td> <td>date</td> </tr> <s:iterator value="weatherList"> <tr> <td><s:property value="city"/></td> <td align="center"><s:property value="temp_lo"/></td> <td align="center"><s:property value="temp_hi"/></td> <td align="center"><s:property value="prcp"/></td> <td><s:property value="date"/></td> </tr> </s:iterator> </table> </body> </html> |
|
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "https://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.devMode" value="true"/> <constant name="struts.allowlist.packageNames" value="test7.sample" /> <package name="test7" extends="struts-default"> <action name="dbtest" class="test7.sample.action.WeatherListAction" method="execute"> <result name="success">/weatherList.jsp</result> </action> </package> <!-- Add addition packages and configuration here. --> </struts> |
|
<?xml version="1.0" encoding="UTF-8"?> <!-- /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ --> <Configuration> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> </Console> </Appenders> <Loggers> <Logger name="org.apache.struts2" level="info"/> <Logger name="test7" level="debug"/> <Root level="warn"> <AppenderRef ref="STDOUT"/> </Root> </Loggers> </Configuration> |
|
<?xml version="1.0" encoding="UTF-8"?> <web-app> <display-name>test7</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.jsp</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>index</welcome-file> </welcome-file-list> </web-app> |
|
<?xml version="1.0" encoding="UTF-8"?> <Context reloadable="true"> <Resource name="mydb" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/mydb" username="testuser" password="userpass" validationQuery="select 1"/> </Context> |
|
<!DOCTYPE html> <html> <head> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=dbtest"> </head> <body> <p>Loading ...</p> </body> </html> |