joy460154849 2018-11-25 08:06 采纳率: 0%
浏览 1223
已结题

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

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条回答

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档