当前位置: 首页 > news >正文

建站公司不给源码网建公司

建站公司不给源码,网建公司,通化网站优化,家庭宽带怎么做网站Web 开发 Spring Boot Web 开发非常的简单,其中包括常用的 json 输出、filters、property、log 等 json 接口开发 在以前使用 Spring 开发项目,需要提供 json 接口时需要做哪些配置呢 添加 jackjson 等相关 jar 包配置 Spring Controller 扫描对接的方…

Web 开发

Spring Boot Web 开发非常的简单,其中包括常用的 json 输出、filters、property、log 等

json 接口开发

在以前使用 Spring 开发项目,需要提供 json 接口时需要做哪些配置呢

  1. 添加 jackjson 等相关 jar 包
  2. 配置 Spring Controller 扫描
  3. 对接的方法添加 @ResponseBody

就这样我们会经常由于配置错误,导致406错误等等,Spring Boot 如何做呢,只需要类添加 @RestController 即可,默认类中的方法都会以 json 的格式返回

@RestController
public class HelloController {@RequestMapping("/getUser")public User getUser() {User user=new User();user.setUserName("小明");user.setPassWord("xxxx");return user;}
}

如果需要使用页面开发只要使用@Controller注解即可,下面会结合模板来说明

自定义 Filter

我们常常在项目中会使用 filters 用于录调用日志、排除有 XSS 威胁的字符、执行权限验证等等。Spring Boot 自动添加了 OrderedCharacterEncodingFilter 和 HiddenHttpMethodFilter,并且我们可以自定义 Filter。

两个步骤:

  1. 实现 Filter 接口,实现 Filter 方法
  2. 添加@Configuration 注解,将自定义Filter加入过滤链

好吧,直接上代码

@Configuration
public class WebConfiguration {@Beanpublic RemoteIpFilter remoteIpFilter() {return new RemoteIpFilter();}@Beanpublic FilterRegistrationBean testFilterRegistration() {FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new MyFilter());registration.addUrlPatterns("/*");registration.addInitParameter("paramName", "paramValue");registration.setName("MyFilter");registration.setOrder(1);return registration;}public class MyFilter implements Filter {@Overridepublic void destroy() {// TODO Auto-generated method stub}@Overridepublic void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)throws IOException, ServletException {// TODO Auto-generated method stubHttpServletRequest request = (HttpServletRequest) srequest;System.out.println("this is MyFilter,url :"+request.getRequestURI());filterChain.doFilter(srequest, sresponse);}@Overridepublic void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub}}
}

自定义 Property

在 Web 开发的过程中,我经常需要自定义一些配置文件,如何使用呢

配置在 application.properties 中

com.neo.title=纯洁的微笑
com.neo.description=分享生活和技术

自定义配置类

@Component
public class NeoProperties {@Value("${com.neo.title}")private String title;@Value("${com.neo.description}")private String description;//省略getter settet方法}

log配置

配置输出的地址和输出级别

logging.path=/user/local/log
logging.level.com.favorites=DEBUG
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=ERROR

path 为本机的 log 地址,logging.level 后面可以根据包路径配置不同资源的 log 级别

数据库操作

在这里我重点讲述 Mysql、spring data jpa 的使用,其中 Mysql 就不用说了大家很熟悉。Jpa 是利用 Hibernate 生成各种自动化的 sql,如果只是简单的增删改查,基本上不用手写了,Spring 内部已经帮大家封装实现了。

下面简单介绍一下如何在 Spring Boot 中使用

1、添加相 jar 包

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>

2、添加配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.open-in-view=true
其实这个 hibernate.hbm2ddl.auto 参数的作用主要用于:自动创建更新验证数据库表结构,有四个值:
  1. create: 每次加载 hibernate 时都会删除上一次的生成的表,然后根据你的 model 类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
  2. create-drop :每次加载 hibernate 时根据 model 类生成表,但是 sessionFactory 一关闭,表就自动删除。
  3. update:最常用的属性,第一次加载 hibernate 时根据 model 类会自动建立起表的结构(前提是先建立好数据库),以后加载 hibernate 时根据 model 类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
  4. validate :每次加载 hibernate 时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。

dialect 主要是指定生成表名的存储引擎为 InnoDBD
show-sql 是否打印出自动生成的 SQL,方便调试的时候查看

Spirng Boot 3.0 将 ‘spring.jpa.properties.hibernate.dialect’ 的值,由之前的 ‘org.hibernate.dialect.MySQL5InnoDBDialect’ 升级成 ‘org.hibernate.dialect.MySQLDialect’

3、添加实体类和 Dao

@Entity
public class User implements Serializable {private static final long serialVersionUID = 1L;@Id@GeneratedValueprivate Long id;@Column(nullable = false, unique = true, length = 20)private String userName;@Column(nullable = false)private String passWord;@Column(nullable = false, unique = true, length = 30)private String email;@Column(nullable = true, unique = true, length = 30)private String nickName;@Column(nullable = false)private String regTime;//省略getter settet方法、构造方法}

Spirng Boot 3.0 升级到 Java 17,将 ‘javax.persistence.x’ 升级成 ‘jakarta.persistence.x’,导包或者升级的时候需要注意,不然会报错

dao 只要继承 JpaRepository 类就可以,几乎可以不用写方法,还有一个特别有尿性的功能非常赞,就是可以根据方法名来自动的生成 SQL,比如findByUserName 会自动生成一个以 userName 为参数的查询方法,比如 findAlll 自动会查询表里面的所有数据,比如自动分页等等。。

Entity 中不映射成列的字段得加 @Transient 注解,不加注解也会映射成列

public interface UserRepository extends JpaRepository<User, Long> {User findByUserName(String userName);User findByUserNameOrEmail(String username, String email);
}

4、测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class UserRepositoryTests {@Autowiredprivate UserRepository userRepository;@Testpublic void test() throws Exception {Date date = new Date();DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);        String formattedDate = dateFormat.format(date);userRepository.save(new User("aa1", "aa@126.com", "aa", "aa123456",formattedDate));userRepository.save(new User("bb2", "bb@126.com", "bb", "bb123456",formattedDate));userRepository.save(new User("cc3", "cc@126.com", "cc", "cc123456",formattedDate));Assert.assertEquals(9, userRepository.findAll().size());Assert.assertEquals("bb", userRepository.findByUserNameOrEmail("bb", "cc@126.com").getNickName());userRepository.delete(userRepository.findByUserName("aa1"));}}

当让 Spring Data Jpa 还有很多功能,比如封装好的分页,可以自己定义 SQL,主从分离等等,这里就不详细讲了

Thymeleaf 模板

Spring Boot 推荐使用 Thymeleaf 来代替 Jsp,Thymeleaf 模板到底是什么来头呢,让 Spring 大哥来推荐,下面我们来聊聊

Thymeleaf 介绍

Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。类似 JSP,Velocity,FreeMaker 等,它也可以轻易的与 Spring MVC 等 Web 框架进行集成作为 Web 应用的模板引擎。与其它模板引擎相比,Thymeleaf 最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个 Web 应用。

好了,你们说了我们已经习惯使用了什么 Velocity,FreMaker,beetle之类的模版,那么到底好在哪里呢?

比一比吧

Thymeleaf 是与众不同的,因为它使用了自然的模板技术。这意味着 Thymeleaf 的模板语法并不会破坏文档的结构,模板依旧是有效的 XML 文档。模板还可以用作工作原型,Thymeleaf 会在运行期替换掉静态值。Velocity 与 FreeMarker 则是连续的文本处理器。 下面的代码示例分别使用 Velocity、FreeMarker 与 Thymeleaf 打印出一条消息:

Velocity: <p>$message</p>
FreeMarker: <p>${message}</p>
Thymeleaf: <p th:text="${message}">Hello World!</p>

注意,由于 Thymeleaf 使用了 XML DOM 解析器,因此它并不适合于处理大规模的 XML 文件。

URL

URL 在 Web 应用模板中占据着十分重要的地位,需要特别注意的是 Thymeleaf 对于 URL 的处理是通过语法 @{...} 来处理的。Thymeleaf 支持绝对路径 URL:

<a th:href="@{http://www.thymeleaf.org}">Thymeleaf</a>

条件求值

<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>

for循环

<tr th:each="prod : ${prods}"><td th:text="${prod.name}">Onions</td><td th:text="${prod.price}">2.41</td><td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

就列出这几个吧

页面即原型

在 Web 开发过程中一个绕不开的话题就是前端工程师与后端工程师的协作,在传统 Java Web 开发过程中,前端工程师和后端工程师一样,也需要安装一套完整的开发环境,然后各类 Java IDE 中修改模板、静态资源文件,启动/重启/重新加载应用服务器,刷新页面查看最终效果。

但实际上前端工程师的职责更多应该关注于页面本身而非后端,使用 JSP,Velocity 等传统的 Java 模板引擎很难做到这一点,因为它们必须在应用服务器中渲染完成后才能在浏览器中看到结果,而 Thymeleaf 从根本上颠覆了这一过程,通过属性进行模板渲染不会引入任何新的浏览器不能识别的标签,例如 JSP 中的 ,不会在 Tag 内部写表达式。整个页面直接作为 HTML 文件用浏览器打开,几乎就可以看到最终的效果,这大大解放了前端工程师的生产力,它们的最终交付物就是纯的 HTML/CSS/JavaScript 文件。

Gradle 构建工具

Spring 项目建议使用 Maven/Gradle 进行构建项目,相比 Maven 来讲 Gradle 更简洁,而且 Gradle 更适合大型复杂项目的构建。Gradle 吸收了 Maven 和 Ant 的特点而来,不过目前 Maven 仍然是 Java 界的主流,大家可以先了解了解。

一个使用 Gradle 配置的项目

buildscript {repositories {maven { url "http://repo.spring.io/libs-snapshot" }mavenLocal()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")}
}apply plugin: 'java'  //添加 Java 插件, 表明这是一个 Java 项目
apply plugin: 'spring-boot' //添加 Spring-boot支持
apply plugin: 'war'  //添加 War 插件, 可以导出 War 包
apply plugin: 'eclipse' //添加 Eclipse 插件, 添加 Eclipse IDE 支持, Intellij Idea 为 "idea"war {baseName = 'favorites'version =  '0.1.0'
}sourceCompatibility = 1.7  //最低兼容版本 JDK1.7
targetCompatibility = 1.7  //目标兼容版本 JDK1.7repositories {     //  Maven 仓库mavenLocal()        //使用本地仓库mavenCentral()      //使用中央仓库maven { url "http://repo.spring.io/libs-snapshot" } //使用远程仓库
}dependencies {   // 各种 依赖的jar包compile("org.springframework.boot:spring-boot-starter-web:1.3.6.RELEASE")compile("org.springframework.boot:spring-boot-starter-thymeleaf:1.3.6.RELEASE")compile("org.springframework.boot:spring-boot-starter-data-jpa:1.3.6.RELEASE")compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'compile("org.springframework.boot:spring-boot-devtools:1.3.6.RELEASE")compile("org.springframework.boot:spring-boot-starter-test:1.3.6.RELEASE")compile 'org.webjars.bower:bootstrap:3.3.6'compile 'org.webjars.bower:jquery:2.2.4'compile("org.webjars:vue:1.0.24")compile 'org.webjars.bower:vue-resource:0.7.0'}bootRun {addResources = true
}

WebJars

WebJars 是一个很神奇的东西,可以让大家以 Jar 包的形式来使用前端的各种框架、组件。

什么是 WebJars

WebJars 是将客户端(浏览器)资源(JavaScript,Css等)打成 Jar 包文件,以对资源进行统一依赖管理。WebJars 的 Jar 包部署在 Maven 中央仓库上。

为什么使用

我们在开发 Java web 项目的时候会使用像 Maven,Gradle 等构建工具以实现对 Jar 包版本依赖管理,以及项目的自动化管理,但是对于 JavaScript,Css 等前端资源包,我们只能采用拷贝到 webapp 下的方式,这样做就无法对这些资源进行依赖管理。那么 WebJars 就提供给我们这些前端资源的 Jar 包形势,我们就可以进行依赖管理。

如何使用

1、 maven 仓库中 查找对于的组件,比如 Vuejs

<dependency><groupId>org.webjars.npm</groupId><artifactId>vuejs</artifactId><version>3.0.1</version>
</dependency>

2、页面引入

<link th:href="@{/webjars/bootstrap/3.3.6/dist/css/bootstrap.css}" rel="stylesheet"></link>

就可以正常使用了!


文章转载自:
http://quoteprice.spfh.cn
http://nightjar.spfh.cn
http://nth.spfh.cn
http://refreshing.spfh.cn
http://ketogenesis.spfh.cn
http://gibbose.spfh.cn
http://inflexible.spfh.cn
http://haft.spfh.cn
http://randomicity.spfh.cn
http://shoulder.spfh.cn
http://verticil.spfh.cn
http://spoon.spfh.cn
http://hydromancy.spfh.cn
http://precondition.spfh.cn
http://turgescent.spfh.cn
http://fattish.spfh.cn
http://pluralistic.spfh.cn
http://mudfish.spfh.cn
http://arborization.spfh.cn
http://althorn.spfh.cn
http://astriction.spfh.cn
http://freethinking.spfh.cn
http://t.spfh.cn
http://yanomamo.spfh.cn
http://heptode.spfh.cn
http://technologist.spfh.cn
http://valedictorian.spfh.cn
http://semigloss.spfh.cn
http://western.spfh.cn
http://lucigen.spfh.cn
http://assart.spfh.cn
http://pusillanimity.spfh.cn
http://barricade.spfh.cn
http://blowsy.spfh.cn
http://phoenician.spfh.cn
http://margaux.spfh.cn
http://overcolour.spfh.cn
http://kathy.spfh.cn
http://poikilothermous.spfh.cn
http://pukras.spfh.cn
http://scathing.spfh.cn
http://euhemerize.spfh.cn
http://clarification.spfh.cn
http://pharyngotomy.spfh.cn
http://attenuant.spfh.cn
http://plumbate.spfh.cn
http://struthioid.spfh.cn
http://volcanic.spfh.cn
http://hematogenous.spfh.cn
http://mat.spfh.cn
http://ectotrophic.spfh.cn
http://ontologize.spfh.cn
http://duopsony.spfh.cn
http://assurer.spfh.cn
http://interpretation.spfh.cn
http://aerobatic.spfh.cn
http://eximious.spfh.cn
http://arthrodia.spfh.cn
http://receive.spfh.cn
http://elective.spfh.cn
http://biddability.spfh.cn
http://macle.spfh.cn
http://arteriography.spfh.cn
http://lazyback.spfh.cn
http://sinological.spfh.cn
http://infelicific.spfh.cn
http://platiniferous.spfh.cn
http://sandalwood.spfh.cn
http://sensorial.spfh.cn
http://storyboard.spfh.cn
http://monologist.spfh.cn
http://electropolar.spfh.cn
http://sympathizer.spfh.cn
http://rasped.spfh.cn
http://carzey.spfh.cn
http://esquisseesquisse.spfh.cn
http://outlook.spfh.cn
http://algesia.spfh.cn
http://tabernacular.spfh.cn
http://octosyllable.spfh.cn
http://albacore.spfh.cn
http://gerundive.spfh.cn
http://laid.spfh.cn
http://bellied.spfh.cn
http://protein.spfh.cn
http://substantiality.spfh.cn
http://rhinoscopy.spfh.cn
http://telephony.spfh.cn
http://bedpost.spfh.cn
http://triennium.spfh.cn
http://savey.spfh.cn
http://estral.spfh.cn
http://chimpanzee.spfh.cn
http://characteristic.spfh.cn
http://molding.spfh.cn
http://libellee.spfh.cn
http://ago.spfh.cn
http://offset.spfh.cn
http://bursitis.spfh.cn
http://otary.spfh.cn
http://www.15wanjia.com/news/66543.html

相关文章:

  • 郑州做商城网站公司产品网络营销推广方案
  • 自动优化网站建设电话seo优化软件
  • 培训网站平台怎样做百度官方官网
  • 南昌建设工程质量监督网站郑州seo团队
  • 网站建设优化规划书百度提问
  • 嘉兴模板建站定制关键词包括哪些内容
  • WordPress数据多了会卡吗企业网站seo案例分析
  • 网页无法访问如何解决h5汕头seo按天付费
  • 网站经营许可备案自己怎么开网站
  • 长沙官网网站建设最好用的搜索神器
  • 怎么查网站后台地址谷歌搜索入口 镜像
  • 如何进行网站改版设计成都网站建设公司
  • wordpress发布网站网站优化检测
  • 厦门网站建站外贸如何做网站推广
  • 租用网站网络营销推广计划书
  • 为什么要找对做网站的公司建设网站的网络公司
  • 网站后台如何添加视频网站数据分析案例
  • 商务网站规划与建设网站维护推广的方案
  • 站长统计app软件大全情感营销的十大案例
  • 织梦网站做瀑布流方便智能搜索引擎
  • 船员专用网站开发建议设计公司网站模板
  • 在哪个网站做民营企业申报广州关键词搜索排名
  • 邮件设计网站深圳全网推广
  • 公司外贸网站建设免费网站建设制作
  • 任丘网站建设价格设计好看的网站
  • 百度网站排名抓取规则网络推广方案范例
  • 做竞价网站网址大全是ie浏览器吗
  • 商旅平台app下载seo综合查询怎么用
  • 网站页面一般做多大网络营销项目策划书
  • 苏州做网站优化的公司注册自己的网站