2017-06-17 57 views
0

例から単純なSpring MVCプロジェクトを作成しましたが、動作しません。私はTomcatを使用しており、がWEB-INF/views/パッケージに表示されず、404エラーが発生します。しかし、index.jspwebappパッケージに入れると、Tomcatが見つけます。それは私のコンフィギュレーションとクラスです:WEB-INF/views/index.jspで404エラーが発生する

Project structure

の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> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>WEB-INF/dispatcher-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.form</url-pattern> 
    </servlet-mapping> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

ディスパッチャ-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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"> 

    <mvc:annotation-driven /> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 
    <context:component-scan base-package="com.mihusle" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

は、メインコントローラ:

package com.mihusle.controller; 

import com.mihusle.model.User; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

/** 
* Created by MHSL on 17.06.2017. 
*/ 
@Controller 
public class MainController { 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public ModelAndView main() { 
     ModelAndView modelAndView = new ModelAndView(); 
     modelAndView.addObject("userJSP", new User()); 
     modelAndView.setViewName("index"); 
     return modelAndView; 
    } 

    @RequestMapping(value = "/check-user") 
    public ModelAndView checkUser(@ModelAttribute("userJSP") User user) { 
     ModelAndView modelAndView = new ModelAndView(); 
     modelAndView.setViewName("secondPage"); 
     modelAndView.addObject("userJSP", user); 
     return modelAndView; 
    } 
} 

ユーザー:

package com.mihusle.model; 

import org.springframework.stereotype.Component; 

/** 
* Created by MHSL on 17.06.2017. 
*/ 
@Component 
public class User { 

    private String name; 
    private String password; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 
} 

のindex.jsp:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags/form" %> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 

<html> 
<body> 
<spring:form method="post" modelAttribute="userJSP" action="check-user"> 

    Name: <spring:input path="name"/> (path="" - указывает путь, используемый в modelAttribute=''. в нашем случае User.name) <br/> 
    Password: <spring:input path="password"/> <br/> 
    <spring:button>Next Page</spring:button> 

</spring:form> 
</body> 
</html> 

助けてください。私は一日中解決策を見つけようと努力しましたが、今ここで何が問題になるのかについての仮定はありません。ありがとう

答えて

0

最初に、ウェルカムページをweb.xmlに設定する必要があります。

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

第二に、は、これまでは、WEB-INFフォルダの下jsphtmlページを置くことはありません。これは、クライアントがアクセスできないファイルを格納するためのものです。

関連する問題