前提・実現したいこと
STSを使って送信フォームを作っています。
tc Serverを起動して、以下のアドレス
http://localhost:8080/MySampleWebApp1/sample
にアクセスしたところ、エラーが発生しました。
発生している問題・エラーメッセージ
HTTPステータス 404 – Not Found -------------------------------------------------------------------------------- Type ステータスレポート メッセージ /MySampleWebApp1/sample 説明 The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. -------------------------------------------------------------------------------- Pivotal tc Runtime 9.0.13.B.RELEASE
該当のソースコード
###application-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Uncomment and add your base-package here: <context:component-scan base-package="org.springframework.samples.service"/> --> <bean id="mybean1" class="jp.base.spring.websample1.MyBean" /> </beans>
###App.java
package jp.base.spring.test1; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { private static ApplicationContext app; public static void main(String[] args) { app = new AnnotationConfigApplicationContext(AutoMyBeanConfig.class); MyBeanInterface bean = app.getBean(MyBeanInterface.class); System.out.println(bean); } }
index.jsp
<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <html> <head> <meta charset="utf-8"> <title>Welcome</title> </head> <body> <h1>Sample Page</h1> <pre>${mybean}</pre> <hr> <form action="sample" method="post"> <input type="text" id="message" name="message"> <input type="submit" value="add"> </form> </body> </html>
pom.xml
<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>jp.base.spring.websample1</groupId> <artifactId>MySampleWebApp1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- Generic properties --> <java.version>1.6</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Web --> <jsp.version>2.2</jsp.version> <jstl.version>1.2</jstl.version> <servlet.version>3.1.0</servlet.version> <!-- Spring --> <spring-framework.version>5.0.0.RELEASE</spring-framework.version> <!-- Logging --> <logback.version>1.2.3</logback.version> <slf4j.version>1.7.25</slf4j.version> <!-- Test --> <junit.version>4.12</junit.version> </properties> <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Other Web dependencies --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <!-- Logging with SLF4J & LogBack --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> <scope>runtime</scope> </dependency> <!-- Test Artifacts --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-framework.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
###web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://xmlns.jcp.org./xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xlmns.jcp.org/xml/ns/javaee" http://xlmns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>MySampleWebApp1</display-name> <!-- - Location of the XML file that defines the root application context. - Applied by ContextLoaderListener. --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
###MySampleServlet.java
package jp.base.spring.websample1; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Servlet implementation class MySampleServlet */ @WebServlet("/sample") public class MySampleServlet extends HttpServlet { private static final long serialVersionUID = 1L; ApplicationContext app; /** * @see Servlet#init(ServletConfig) */ @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); app = new ClassPathXmlApplicationContext("/spring/application-config.xml"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MyBean mybean1 = (MyBean)app.getBean("mybean1"); request.setAttribute("mybean", mybean1); request.getRequestDispatcher("/index.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String message = request.getParameter("message"); MyBean mybean1 = (MyBean)app.getBean("mybean1"); mybean1.addMessage(message); response.sendRedirect("sample"); } }
###bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mybean1" class="jp.base.spring.test1.MyBean2"> <property name="message" value="It's new Bean!"></property> </bean> </beans>
試したこと
エラーメッセージには、
サーバーが見つからない、または、見られるサーバーが存在しないとありますが、
STS内蔵のtc Serverは起動しているため、考えにくいです。
Window / Preferences / Java / Installed JREs から、
Installed JREsの欄に、
JRE home : C:\Program Files\Java\jre1.8.0_191
JRE name : jre1.8.0_191
を追加して、チェックボックスにチェックを入れました。
補足情報(FW/ツールのバージョンなど)
OS : Windows 10
STS : sts-3.9.7.RELEASE
書籍 : Spring Framework 5 プログラミング入門 (秀和システム)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。