STS映射控制器查看

时间:2016-02-24 13:49:34

标签: java spring jsp

我正在学习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映射

localhost:8080 / show“hello world”

但结果是“回家”

所以没有映射

我想解决它。

帮助我。

1 个答案:

答案 0 :(得分:1)

如果你想渲染一个视图 - &gt;意思是你想要显示home.jsp页面,你需要将类注释从@RestController更改为@Controller。同 @RestController你返回JSON或XML,也取消注释servlet-context.xml中的internalViewResolver Bean。