2016-02-24 86 views
0

我學習STSSTS映射器來查看

我開始與JPA啓動項目,網絡

我的代碼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"; 
} 
} 

and servlet-context.xml is

<?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」 的

,但結果是 「家」

所以沒有映射

我想解決這個問題。

幫助我。

回答

1

如果你想呈現一個視圖 - >意思是你想顯示home.jsp頁面,你需要將你的類註解從@RestController改爲@Controller。用 @RestController返回JSON或XML,還取消servlet-context.xml中的internalViewResolver Bean的註釋。

+0

by @RestController - > @ Controller,錯誤信息已更改。謝謝 –