|
解凍するファイル
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 |
|
コピー元
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 |
|
package test4.sample.action; import java.util.Map; import org.apache.struts2.ActionSupport; import org.apache.struts2.action.SessionAware; import org.apache.struts2.interceptor.parameter.StrutsParameter; public class AuthenticationAction extends ActionSupport implements SessionAware { private Map<String, Object> sessionMap; private String userName; private String password; private boolean isAuthenticated = false; public String execute() throws Exception { return SUCCESS; } public String login() { String loggedUserName = null; // check if the userName is already stored in the session if (sessionMap.containsKey("userName")) { loggedUserName = (String) sessionMap.get("userName"); } if (loggedUserName != null //&& loggedUserName.equals("admin") ) { //System.out.println("loggedUserName is not null"); return SUCCESS; // return welcome page } // if no userName stored in the session, // check the entered userName and password if (userName != null //&& userName.equals("admin") //&& password != null //&& password.equals("adminpasswd") && isAuthenticated) { // add userName to the session sessionMap.put("userName", userName); //System.out.println("isAuthenticated is true.userName(" + userName + ")"); return SUCCESS; // return welcome page } // in other cases, return login page return INPUT; } public void validate(){ if ( userName != null && userName.length() == 0 ){ addFieldError( "userName", "UserName is required." ); isAuthenticated = false; } if ( password != null && password.length() == 0 ){ addFieldError( "password", "password is required." ); isAuthenticated = false; } if (userName != null && userName.length() > 0 && !(userName.equals("admin"))) { addFieldError( "password", "UserName or password is not correct." ); isAuthenticated = false; } if (userName != null && userName.length() > 0 && userName.equals("admin") && password != null && password.length() > 0 && !(password.equals("adminpasswd"))) { addFieldError( "password", "UserName or password is not correct." ); isAuthenticated = false; } else { isAuthenticated = true; } } public String logout() { // remove userName from the session if (sessionMap.containsKey("userName")) { sessionMap.remove("userName"); } isAuthenticated = false; //System.out.println("isAuthenticated is " + isAuthenticated); return SUCCESS; } // public void setSession(Map sessionMap) { // this.sessionMap = sessionMap; // } public void withSession(Map session) { sessionMap = session; } @StrutsParameter(depth = 1) public void setUserName(String userName) { this.userName = userName; } @StrutsParameter(depth = 1) public void setPassword(String password) { this.password = password; } @StrutsParameter(depth = 1) public String getUserName() { return userName; } @StrutsParameter(depth = 1) public String getPassword() { return password; } } |
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts2 Login</title> <s:head /> </head> <body> <center> <h3>Login</h3> <s:form namespace='' action="login" method="post"> <s:textfield name="userName" label="Enter User Name" /> <s:password name="password" label="Enter Password" /> <s:submit label="Login" /> </s:form> </center> </body> </html> |
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome</title> </head> <body> <center> <h3>Welcome you have logged in successfully!</h3> <h3>Welcome <i>${sessionScope.userName}</i>, you have logged in successfully!</h3> <p>I've said hello to you <s:property value="#session.userName" /></p> <%-- --%> <h3><a href="logout">Logout</a></h3> </center> </body> </html> |
|
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.allowlist.packageNames" value="test4.sample" /> <package name="test4" extends="struts-default"> <action name="index" class="test4.sample.action.AuthenticationAction" method="execute"> <result name="success">/Login.jsp</result> <result name="input">/Login.jsp</result> </action> <action name="login" class="test4.sample.action.AuthenticationAction" method="login"> <result name="success">/Welcome.jsp</result> <result name="input">/Login.jsp</result> </action> <action name="logout" class="test4.sample.action.AuthenticationAction" method="logout"> <result name="success">/Login.jsp</result> </action> </package> </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="test4" level="debug"/> <Root level="warn"> <AppenderRef ref="STDOUT"/> </Root> </Loggers> </Configuration> |
|
<?xml version="1.0" encoding="UTF-8"?> <web-app> <display-name>test4</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> |
|
<!DOCTYPE html> <html> <head> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=index"> </head> <body> <p>Loading ...</p> </body> </html> |