maven - I get a 404 error when trying to access a .jsp file under my WEB-INF directory and I can't figure out why -
i've looked @ every post find on issue every time seems configurations should be.
when type in url http:/localhost:8080/ correct index.jsp displayed, when type in http:/localhost:8080/account 404 error.
any advice appreciated!
project structure
projectname -v .idea -v src ---v main -----v java -------v springmvc.controllers ---------> accountcontroller.java -----v resources -------> application-context.xml -------> log4j.properties -----v webapp -------v web-inf ---------v views -----------> account.jsp ---------> web.xml -------> index.jsp -> pom.xml
accountcontroller.java
package springmvc.controllers; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.servlet.modelandview; @controller @requestmapping("/account") public class accountcontroller { @requestmapping(value = "/account", method = requestmethod.get) public modelandview findallaccounts() throws exception { modelandview mav = new modelandview(); mav.setviewname("account"); mav.addobject("sometext", "listing accounts!"); return mav; } @requestmapping(value="/{accountid}", method = requestmethod.get) public modelandview findaccount(@pathvariable int accountid, model model) { modelandview mav = new modelandview(); mav.setviewname("account"); mav.addobject("sometext", string.format("showing account %d", accountid)); return mav; } }
application-context.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" xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="springmvc.controllers" /> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
account.jsp
<%@ page contenttype="text/html;charset=utf-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <h1>${sometext}</h1>
web.xml
<!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>archetype created web application</display-name> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>springdispatcherservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:application-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springdispatcherservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
index.jsp
<%@ page import="java.util.calendar" %> <%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <% calendar cal = calendar.getinstance();%> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>jsp page</title> </head> <body> <h1>hello, time <%= cal.gettime() %> </h1> </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/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.project.projectname</groupid> <artifactid>projectname</artifactid> <packaging>war</packaging> <version>1.0-snapshot</version> <name>springmvc maven webapp</name> <url>http://maven.apache.org</url> <properties> <org.springframework.version>4.0.6.release</org.springframework.version> <hibernate.version>5.2.1.final</hibernate.version> <mysql.connector.version>5.5.49</mysql.connector.version> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${org.springframework.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${org.springframework.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>${org.springframework.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-orm</artifactid> <version>${org.springframework.version}</version> </dependency> <dependency> <groupid>jstl</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> </dependencies> <build> <finalname>root</finalname> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
you have added @requestmapping("/account") public class accountcontroller {
above controller class, therefore controller map requests /accounts header only,
have added
@requestmapping(value = "/account", method = requestmethod.get)
in method also, controller looking accounts/accounts/ requests execute particular method.
try http:/localhost:8080/account/account or remove class level request mapping.
Comments
Post a Comment