gpt4 book ai didi

java - STS映射 Controller 查看

转载 作者:行者123 更新时间:2023-12-01 10:21:13 25 4
gpt4 key购买 nike

我正在学习STS

我开始使用 jpa,web 启动项目

我的代码 SpringProjectApplication.java 是

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringProjectApplication {

public static void main(String[] args) {
SpringApplication.run(SpringProjectApplication.class, args);
}
}

我的 Homecontroller.java 是

package com.example;


import java.util.Locale;

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

/**
* Handles requests for the application home page.
*/
@RestController
public class HomeController {


/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping("/")
public String helloWorld(Locale locale, Model model) {
return "home";
}



@RequestMapping(value = "/login", method = RequestMethod.POST)
public String verifyLogin(Locale locale, Model model) {

return "login";
}

@RequestMapping(value = "/SignUp", method = RequestMethod.GET)
public String SignUp(Locale locale, Model model) {

return "SignUp";
}
}

servlet-context.xml 是

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

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="come.example" />

而 home.jsp 是

enter image description here

总体结构为

enter image description here

我想如果 homecontroller 与 home.jsp 映射

本地主机:8080/显示“ Hello World ”

但结果是“家”

所以没有映射

我想解决这个问题。

帮助我。

最佳答案

如果你想渲染一个 View -> 意味着你想显示 home.jsp 页面,你需要将你的类注释从 @RestController 更改为 @Controller。和@RestController u 返回 JSON 或 XML,同时取消 servlet-context.xml 中的internalViewResolver Bean 注释。

关于java - STS映射 Controller 查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35604008/

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