【问题标题】:Can't configure Spring MVC application无法配置 Spring MVC 应用程序
【发布时间】:2015-08-15 14:50:07
【问题描述】:

我在配置 Spring MVC 应用程序时遇到了很大的问题。无论我在配置 xml 中写什么,在到达 localhost/appname/ 或 localhost/appname/register 时都会得到 404。为什么会这样?阅读 Maven 中的 MVC 教程,看起来我正在按照他们说的做所有事情。

web.xml

<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Wymysl to!</display-name>




    <servlet>
    <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

调度程序-servlet.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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="wymysl.Controllers" /> 
    <context:annotation-config />

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

     <bean
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>



</beans>

RegisterController.java

package wymysl.Controllers;



import org.springframework.beans.factory.annotation.Autowired;
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 wymysl.database.Person;
import wymysl.database.PersonsRepository;


@Controller
public class RegisterController {

    @Autowired
    PersonsRepository repo;

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register() {


        return "register";

    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@ModelAttribute("Person") Person person) {

        System.out.println(""+person.getName());

        return "index";

    }



}

【问题讨论】:

  • 其实你没有映射“/”。尝试拨打localhost/appname/register
  • 也试过了,但没有效果
  • @Mike Raphael 是对的,您的控制器中没有这样的“/”映射,@RequestMapping("/") public String showIndexPage() { return "index"; }
  • @Mike 还有一件事,当我在本地(通过 Tomcat 7)运行我的网络应用程序时,我的 URL 还包括我在 Tomcat 中使用的端口号,即:localhost:8080/appname/登记。不确定这是否是绝对必要的,但这就是我在本地找到我的 jsps 的方式可能值得一试
  • @smoggers 好点。

标签: java spring maven spring-mvc web-applications


【解决方案1】:

您可以更改以使其更基本的内容:-

  1. 删除

    上下文配置位置 /WEB-INF/dispatcher-servlet.xml

        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    

将您的 web.xml 和 dispatcher-servlet.xml 放在同一路径。

  1. 不需要 &lt;context:annotation-config /&gt; 因为它已经被组件扫描处理了。见这里:- Difference between <context:annotation-config> vs <context:component-scan>&lt;bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/&gt;

  2. 确保 register.jsp 存在于 /WEB-INF/jsp/register.jsp 中。

如果问题仍未解决,您需要分享您的服务器日志。

【讨论】:

  • 我根据您的建议进行了更改,现在我的应用程序至少在 localhost/appname/ url 上启动,但除 /index.jsp 之外的所有其他 url 都给出 404。此外,index.jsp 被加载为原始 html 而没有导入的css文件。在编写 web.xml 和 dispatcher-servlet.xml 之前,当我可以在 tomcat 中通过直接路径打开这些文件时,一切正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-12
  • 2021-09-13
  • 2016-08-18
相关资源
最近更新 更多