首页 > 其他分享 >全注解springMVC实例20230517

全注解springMVC实例20230517

时间:2023-05-17 14:23:18浏览次数:54  
标签:springMVC org springframework public 20230517 import 注解 servlet annotation

 

1、pom
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!--Spring and Transactions-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!--Logging with SLF4j & LogBack-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>

<build>
<finalName>testspringmvc</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

 

 

2、WebConfig

package com.mvc.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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("com.mvc.controller")
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
}
}


3、DataConfig
package com.mvc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;

@Configuration
public class DataConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.build();
}

@Bean
public JdbcOperations jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

4、RootConfig
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.core.type.filter.RegexPatternTypeFilter;
import java.util.regex.Pattern;

@Configuration
@Import(DataConfig.class)
@ComponentScan(basePackages = {"com.mvc"},
excludeFilters = @ComponentScan.Filter(type=FilterType.CUSTOM, value=RootConfig.WebPackage.class))
public class RootConfig {
public static class WebPackage extends RegexPatternTypeFilter {
public WebPackage() {
super(Pattern.compile("com.m24.controller"));
}
}
}

 

5、schema.sql
create table User (
id int primary key,
username varchar(20) unique not null,
password varchar(20) not null,
first_name varchar(30) not null,
last_name varchar(30) not null,
email varchar(30) not null
);
insert into User values(100

 

 

 

 

 

 

 

源码:  springmvcdemo 

转https://www.cnblogs.com/m2492565210/p/8328070.html

标签:springMVC,org,springframework,public,20230517,import,注解,servlet,annotation
From: https://www.cnblogs.com/smallfa/p/17408606.html

相关文章

  • @Builder 注解的简单使用
    1、导语(可跳过)Java小白一枚,研读公司代码。发现实体类上加了@Builder、@NoArgsConstructor和@AllArgsConstructor。话不多说,上代码 2、实体类代码@Data@EqualsAndHashCode(callSuper=false)@Builder@AllArgsConstructor@NoArgsConstructor@ApiModel(value="TOrder对......
  • @ExceptionHandler注解
    1,基本使用方法Spring的@ExceptionHandler可以用来统一处理方法抛出的异常,比如这样:@ExceptionHandler()publicStringhandleExeption2(Exceptionex){System.out.println("抛异常了:"+ex);ex.printStackTrace();StringresultStr="异常:默认";returnr......
  • 又踩坑了!SpringMVC+ant+tomcat实现自动化打包,原来这么多坑
    背景是项目使用的是tomcat进行打包,没有使用Maven,需要构建一个ant执行的文件,用于后期可以在实际中使用,运行的平台是Windows。在使用中遇到了不少的坑点,一一记录下来,方便后期或者和我遇到相同问题的朋友们查看和借鉴。错误:打包报错[2023-05-16 10:35:23,164] Artifact meta......
  • springmvc学习。开发一个小网站
    一、准备在Intelij里面选择maven项目,然后选择webapp,否则无法整整build。一共两个配置文件web.xml和springmvc.xml目录一共有3个。在main下有javaresources(springmvc.xml文件在当下)webapp.(放js。img。jsp等) 二、POM添加依赖版本5.2.10spring-contextspri......
  • springmvc上传文件——疯狂踩坑
    添加依赖: 配置:注意这里bean的id必须为 multipartResolver 实现:这里需要注意圈起来的地方的名字和文件上传时候的字段名称需要保持一致  测试:这里键名必须为upload和上面那张图中的upload对应publicResultuploadResFile(@RequestParamMultipartFileupload,Ht......
  • SpringMVC快速复习(超详细)
    目录一、SpringMVC简介1、什么是MVC2、什么是SpringMVC3、SpringMVC的特点二、HelloWorld1、开发环境2、创建maven工程a>添加web模块b>打包方式:warc>引入依赖3、配置web.xmla>默认配置方式b>扩展配置方式4、创建请求控制器5、创建springMVC的配置文件6、测试HelloWorlda>实现对首......
  • SpringBoot 使用 Sa-Token 完成注解鉴权功能
    注解鉴权——优雅的将鉴权与业务代码分离。本篇我们将介绍在Sa-Token中如何通过注解完成权限校验。Sa-Token是一个轻量级java权限认证框架,主要解决登录认证、权限认证、单点登录、OAuth2、微服务网关鉴权等一系列权限相关问题。Gitee开源地址:https://gitee.com/dromar......
  • C# opc ua客户端实例源码,带ef6+sqlite。 代码有完整的注解,及包括所有
    C#opcua客户端实例源码,带ef6+sqlite。代码有完整的注解,及包括所有的链接库和程序结构思维图。纯学习资料YID:2855638904489888......
  • SpringMVC中多个拦截器的执行顺序
    在SpringMVC中,多个拦截器的执行顺序是由配置文件中拦截器的顺序来决定的。假设我们有3个拦截器:Interceptor1、Interceptor2、Interceptor3,通过配置文件的方式定义拦截器的顺序,例如:<mvc:interceptors><mvc:interceptor><mvc:mappingpath="/**"/><bean......
  • 注解实现:判空赋值
    工作中的小玩意~~流程:注解实现反射工具类注解定义及实现注解定义:@Documented@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public@interfaceCheckParam{Stringvalue()default"-1";}简单解释上述其相关注解Target:描述了注解修饰的对象......