简体   繁体   English

STS映射控制器查看

[英]STS mapping controller to view

I am studying STS 我正在学习STS

I am started starter project with jpa,web 我从jpa,web开始了启动项目

my code SpringProjectApplication.java is 我的代码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);
}
}

and my Homecontroller.java is 而我的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";
}
}

and servlet-context.xml is 和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" />

and home.jsp is 和home.jsp是

在此处输入图片说明

overall structure is 整体结构是

在此处输入图片说明

I think if homecontroller is mapped with home.jsp 我认为homecontroller是否与home.jsp映射

localhost:8080/ show "hello world" 本地主机:8080 /显示“你好,世界”

but result is "home" 但结果是“家”

so not mapped 所以没有映射

I want to solve it. 我想解决。

help me. 帮我。

If you want to render a view -> meaning you want to show home.jsp page you need to change your class annotation from @RestController to @Controller. 如果要渲染视图->表示要显示home.jsp页面,则需要将类注释从@RestController更改为@Controller。 With @RestController u return JSON or XML, also uncomment internalViewResolver Bean in your servlet-context.xml. 使用@RestController,您可以返回JSON或XML,还可以在servlet-context.xml中取消注释internalViewResolver Bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM