gpt4 book ai didi

java - 无法配置 Spring MVC 应用程序

转载 作者:行者123 更新时间:2023-11-30 08:00:27 25 4
gpt4 key购买 nike

我在配置 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";

}



}

ProjectTree

最佳答案

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

  1. 删除

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

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

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

  • 不需要 <context:annotation-config />因为它已经被组件扫描处理了。请参阅此处:- Difference between <context:annotation-config> vs <context:component-scan><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

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

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

    关于java - 无法配置 Spring MVC 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025850/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com