joy460154849
joy460154849
采纳率10.5%
2018-11-25 08:06 阅读 1.2k

springmvc 注解方式配置时,没有了web.xml,如何内嵌jetty

5

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
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.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan( "com")
public class WebConfig extends WebMvcConfigurerAdapter{

/**
 *配置静态资源处理 
 *
 */
public void configureDefaultServerletHandling(DefaultServletHandlerConfigurer configurer){
    configurer.enable();
}

/**
 *配置视图解析器 
 */
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPath("/WEB-INF/");
    return configurer;
}

@Bean
public FreeMarkerViewResolver freeMarkerViewResover(){
    FreeMarkerViewResolver freeMarker = new FreeMarkerViewResolver();
    freeMarker.setSuffix(".ftl");
    freeMarker.setContentType("utf-8");
    return freeMarker;
}

/**
 * 配置multipart解析器
 */
@Bean
public MultipartResolver multipartResolver(){
    return new StandardServletMultipartResolver();
}

}


package 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(excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public interface RootConfig {

}


package com.controller;

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

@Controller
public class ControllerDemo {

@RequestMapping("/demo/list")
public String demo(Model model){
    model.addAttribute("welcom", "welcom to use spring demo");
    return "demo/index";
}

}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • qq_37886086 欧阳惜竹 2018-11-25 08:24

    在项目的跟目录执行mvn clean jetty:run 运行项目
    详情请参考https://blog.csdn.net/mn960mn/article/details/60465951

    点赞 评论 复制链接分享
  • huanyingchangmaoshou huanyingchangmaoshou 2018-12-20 16:08

    无web.xml,不用maven配置,直接用java main方法启动jetty,然后使用spring注解。您找到实现方式了么?

    点赞 评论 复制链接分享

相关推荐