gpt4 book ai didi

java - Spring Controller 不工作

转载 作者:行者123 更新时间:2023-11-30 07:11:38 24 4
gpt4 key购买 nike

当我尝试打开“greeting.html”时,出现错误 404

以下是文件:

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Archetype Created Web Application</display-name>

<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<!-- mapped all incoming requests to html -->
<url-pattern>*.html</url-pattern>
</servlet-mapping>

</web-app>

servlet-config.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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-4.3.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="com.rahul.controller" />

<!-- mentions where all the JSPs are located at -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp" p:suffix=".jsp"></bean>

</beans>

HelloController.java

package com.rahul.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

@RequestMapping(value = "/greeting")
public String sayHello(Model model) {
model.addAttribute("greeting", "hello world");
return "hello";
}

}

hello.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>${greeting}</h1>

</body>
</html>

我的http请求GET localhost:8080/FitnessTracker/greeting.html

我做错了什么?

最佳答案

尝试以下重构:

@Controller
public class HelloController {

@RequestMapping(value = "/greeting")
public Model sayHello() {
model.addAttribute("greeting", "hello world");
return model;
}
}

或请求:

http://localhost:8080/fitTrackerServlet/gree‌​ting.html

关于java - Spring Controller 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39142722/

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