gpt4 book ai didi

java - 为什么 org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping 不起作用?

转载 作者:太空宇宙 更新时间:2023-11-04 12:17:08 25 4
gpt4 key购买 nike

我是 Spring Mvc 的初学者。我已经使用 BeanNameUrlHandlerMapping 。完成所有映射后,当我从 url 调用页面时,会发生 404 错误。我将如何解决这个问题。

spring-dispatcher-servlet.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<!-- Default hander mapping -->
<!-- handler -->

<bean id="HandlerMapping" class=" org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- controller -->

<bean name="/hello.html" class="main.Hellocontroller"></bean>







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

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Spring</display-name>

<!-- Default Configuration -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>


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

</servlet-mapping>
</web-app>

Hellocontroller.java

主包;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class Hellocontroller extends AbstractController {

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub


ModelAndView mdvAndView=new ModelAndView("Hellopage");
mdvAndView.addObject("messege","Welcome .View this page");

return mdvAndView;
}

}

Hellopage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1>Hiiii</h1>
<h1>


${messege}

</h1>
</body>
</html>

最佳答案

我试图通过修改您的代码来使事情变得清晰。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<context:component-scan base-package="com.example.springmvc" /> <!-- base package where spring will scan for Component like Controller,Service,Repository etc-->
<mvc:annotation-driven/>

<!-- View Reserver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

你的jsp页面应该在WEB-INF目录中。请注意,前缀应该是“/WEB-INF/”而不是“/WEB-INF”

您的 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Spring</display-name>

<!-- Default Configuration -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

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

</servlet-mapping>
</web-app>

你的 Controller

@Controller
public class HelloController {

@RequestMapping(value = {"/","/home"})
public String index(){
return "index";
}

@RequestMapping(value = {"/welcome"})
public String index(ModelMap model){
model.addAttribute("message","Welcome to Web Application!");
return "Hellopage";
}

}

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Home Page </h1>
<a href="${pageContext.servletContext.contextPath}/welcome">Click Here</a>
</body>
</html>

Hellopage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<h1>Hello Page </h1>
${requestScope.message}
</body>
</html>

确保您的 jsp 页面位于 WEB-INF 目录中。

关于java - 为什么 org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39265870/

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