springboot案例helloworld,分页插件

  • springboot项目打成war包:
    参考 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins, https://blog.csdn.net/qq_33512843/article/details/80951741

文章目录

  • 1, 配置pom.xml
  • 2, 配置Application.java和Controller类
    • a, 自定义启动类Application.java
    • b,controller类
  • 3, 分页插件
  • 4, 报错Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lan

在这里插入图片描述
app类:单独启动(方便在idea中调试)

################## 配置pom.xml ---------start-------###############
a
b
1.0-SNAPSHOT


	org.springframework.boot
	spring-boot-starter-parent
	2.0.6.RELEASE
	 



	
		org.springframework.boot
		spring-boot-starter-tomcat
	
	
		org.springframework.boot
		spring-boot-starter-jdbc
	
	
		org.springframework.boot
		spring-boot-starter-web
	

################## 配置pom.xml ---------end-------###############
@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

tomcatapp类:tomcat中启动(上线测试)

################## 配置pom.xml ---------start-------###############
##groupId, artifactId,version
war

....


mywar

		
		org.springframework.boot
		spring-boot-maven-plugin
		


################## 配置pom.xml ---------end-------###############
public class TomcatApplication extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}
}

在idea下, 创建java项目, 配置maven支持

1, 配置pom.xml

 
		org.springframework.boot
		spring-boot-starter-parent
		2.2.6.RELEASE
		 
	
	com.example
	demo
	0.0.1-SNAPSHOT
	demo
	war
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-tomcat
			 
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin



			
		
		springboot 
	

2, 配置Application.java和Controller类

a, 自定义启动类Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("a") //扫描controller所在的包
public class Application extends SpringBootServletInitializer {

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

    @Override
    public SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 设置启动类,用于独立tomcat运行的入口
        return builder.sources(Application.class);
    }
}

b,controller类

package a;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import spark_jobctl.JobKill;
import spark_jobctl.Submit_launcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class HelloController {

    @RequestMapping(value = "/success", method = {RequestMethod.GET})
    @ResponseBody
    public String successModel(HttpServletRequest request, HttpServletResponse reponse) {
        return "success";
    }
}

3, 分页插件


    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.5


#### serviceImpl实现分页
ArrayList>  list = new ArrayList>();
try {
    //分页参数
    Integer page_num = Integer.valueOf(map.get("page_num").toString());
    Integer page_size = Integer.valueOf(map.get("page_size").toString());
    PageHelper.startPage(page_num, page_size);
    //获取mapper返回的数据
    Map data = disMapper.getData();
    list.add(data);
} catch (Exception e) {
    e.printStackTrace();
    return "错误!"
}
PageInfo> pageInfo = new PageInfo<>(list);
return n "sucess";

4, 报错Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lan

解决: 在打war包时,排除掉tomcat*.jar, javax*.jar, 然后再重新打包

你可能感兴趣的:(编程语言-java-ssm框架)