gpt4 book ai didi

java - spring mvc 4 中页面未找到错误,尽管它存在

转载 作者:行者123 更新时间:2023-12-01 11:12:35 27 4
gpt4 key购买 nike

我正在使用 spring mvc 4 开发一个小型应用程序,它获取用户的输入并将其插入数据库。这是dispatcher-servlet.xml

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

<context:component-scan base-package="controllers.FormController" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />

</beans>

这是我的 Controller

    @Controller
@RequestMapping("/form")
public class FormController {

@Autowired
private FormService formService;

public FormService getFormService() {
return formService;
}

public void setFormService(FormService formService) {
this.formService = formService;
}
@RequestMapping(value = "/userForm", method = RequestMethod.GET)
public String showUser(Model model){
User user = new User();
model.addAttribute("userForm", user);
return "index";
}
@RequestMapping(value = "/adduser", method = RequestMethod.POST)
public String addUser(@ModelAttribute("userForm") User user, BindingResult result, Model model){
formService.insert(user.getUsername(), user.getPassword());
return "success";
}
}

当我在浏览器中输入以下网址“http://localhost:8080/FormSpring/form/userForm.htm ”时,它显示页面未找到错误,尽管成功页面位于 WEB-INF/jsp 目录中

这是 web.xml 文件

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<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>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

此外,项目的结构如下图所示

enter image description here

最佳答案

您的 Controller 映射到adduser,而不是adduser.htm!同样在浏览器中,您执行 GET,而不是 POST 请求。

关于java - spring mvc 4 中页面未找到错误,尽管它存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32178115/

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