java - JSP 页面在浏览器中作为文本

标签 java spring jsp

我正在阅读 Spring in Action 并尝试重复书中的代码。当我使用 html 而不是 jsp 时它工作得很好。但问题是,当我启动服务器时,我看到网页中安装了jsp的源代码。 我的源代码是:

网络配置

package spittr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;



@Configuration
@EnableWebMvc
@ComponentScan(value = "spittr.web")
public class WebConfig
        extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver =
                new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

根配置

    package spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;


@Configuration
@ComponentScan(basePackages={"spittr"},
        excludeFilters={
                @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
        })
public class RootConfig {
}

SpittrWebAppInitializer

    package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;


public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }
}

申请

package spittr.config;

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

/**
 * Created by vadim on 10/5/16.
 */
@SpringBootApplication
public class Application {

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

家庭 Controller

package spittr.web;

import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

    @RequestMapping(value="/", method=GET)
    public String home() {
        return "home";
    }
}

项目结构为image structure

最佳答案

我修好了。要运行我的程序,只需删除应用程序并使用 TomCat 运行程序(带有分解的工件)。

关于java - JSP 页面在浏览器中作为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39916117/

相关文章:

java - 如何在服务器端查找手机型号和制造商名称?

spring - cvc-complex-type.2.4.c : The matching wildcard is strict, 但找不到元素 'jms:listener-container' 的声明

java - 无法映射我的 Spring Maven 项目的资源

Spring jsp 形式的 Eclipse 警告 “List is a raw type”

java - Spark 数据帧否定过滤条件

java - 域对象?业务对象?模型?领域模型?有什么区别?

java - 向 PMD 添加规则集?

java - Guava RateLimiter 爆破

Java ImageIO.write() 最多需要 6 秒

java - 如何在制作 jdbc 简单程序时消除此错误