首页 > 编程知识 正文

intellij idea创建java项目,eclipse导入idea的web项目

时间:2023-05-04 15:52:01 阅读:144360 作者:4440

SpringBoot项目入门(Eclipse创建项目)最近公司的项目不着急,所以有很多时间学习新知识。 最近在网上发现SpringBoot非常受欢迎,所以自己试着做一个SpringBoot项目,分享它的疑问和解决方案,方便以后看,也方便新的学习者,减少痛苦。

因为是第一次写博客,如果有问题的话请指出来。 (本教程需要了解spring依赖注入、springMVC和web开发。)

springBoot简介

spring公式加快了项目框架的构建,使开发人员更容易开发业务代码,而无需重复定义模板化配置。

插件安装

因为spring官方提供了一个名为STS的插件,所以开发springBoot项目非常容易,所以首先要安装STS插件。

打开Eclipse,选择Help/EclipseMarketspace以打开插件市场,然后在图中输入STS搜索插件。

必须单击install安装,并在安装完成后重新启动。 然后,可以通过插件创建springboot项目

创建项目

在Eclipse的File/New/Other输入框中搜索sping,可以看到以下内容:

输入项目的信息。

单击Next:

springboot版本,选择我选择的2.0.4。 选择web相关性,然后单击finish,即可创建springboot。

项目的目录结构在上图中进行了说明,我们来看看pom.xml文件的依赖关系:

ependenciesdependencygroupidorg.spring framework.boot/groupidartifactidspring-boot-starter-web/artifact id/ependencydependencygroupidorg.spring framework.boot/groupidartifactidspring-boot-starter-test/artifactidscopeteseses 到此为止,您可以看到在我们创建项目时选择的web在此自动添加了web依赖关系,并且在设备测试中默认添加了web依赖关系。

项目已创建。 接下来测试项目是否可以正常运行。 由于spring不正式建议使用JSP,而是使用默认的thymeleaf作为模板,因此以下页面也将使用thymeleaf。

首先,必须将thymeleaf依赖关系添加到pom.xml文件中

! -- Thymeleaf组件---从属cygroupidorg.spring framework.boot/groupidartifactidspring-boot-starter-thyme learaf

@ components can (base packages={ ' com.yss.com mon ' } )我的包结构是这样的,因为我没有指定其他包:

新建User.java实体类

公共类用户{私有用户名称; 隐私字符串sex; 隐私字符串hobby; 公共用户() { super ); }公共用户(string name、String sex、String hobby ) { super ); this.name=name; this.sex=sex; this.hobby=霍比; } public String getName (() { return name; }公共语音集名称(字符串名称) { this.name=name; }公共字符串get sex () { return sex; }公共语音集sex (

String sex) { this.sex = sex; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; }}

新建UserController.java

@Controllerpublic class UserController { @RequestMapping(value="/user") public String index(Model model) { List<User> users = new ArrayList<User>(); users.add(new User("皮皮虾", "男", "喜欢皮")); users.add(new User("dsdxx", "男", "砍树")); users.add(new User("佩奇", "男", "叫")); model.addAttribute("users",users); return "user"; }}

这里是用@Controller注入spring成为一个控制器,在SpringBoot中还有个注解是@ResController其实就是@ResponseBody和@Controller的结合体,该控制器所有方法不返回视图,只返回数据,和你在方法上添加@ResponseBody一样。
SpringBoot默认返回thymeleaf模板视图,所以这里方法返回user默认是去templates下去找user.html,当然,JSP也是可以配置的,我们就不配置了,再说springboot官方也不推荐JSP,代码耦合度太高。

template文件夹下新建user.html文件:

<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head> <meta charset="UTF-8"> <title >Title</title></head><body> <table> <tr> <td>姓名</td> <td>性别</td> <td>爱好</td> <td>性别</td> </tr> <tr th:each="user:${users}" th:object="${user}"> <td th:text="${user.name}"></td> <td th:text="${user.sex}"></td> <td th:text="${user.hobby}"></td> <td th:text="*{sex}"></td> </tr> </table></body></html>

注意这里需要加上 thymeleaf命名空间,不然不能使用th:each等,这个是thymeleaf语法,有兴趣的同学可以自己学习,使用起来和EL,JSTL很像。
好了所以文件都创建完成了。
右击springBoot入口文件,启动项目,这样我们就可以通过http://localhost:8080/user访问了:

后续:
我们可以在springBoot文件中修改一些默认配置,当前我修改的一些:

#项目访问根路径server.servlet.context-path=/demo_sxh#端口server.port=8888server.tomcat.uri-encoding=UTF-8#thymeleaf缓存spring.thymeleaf.cache=false #热编译 spring.devtools.restart.enabled=true #热编译监听目录spring.devtools.restart.additional-paths=src/main/java #国际化配置spring.messages.basename=i18n/messagesspring.messages.encoding=UTF-8#logging.level.com.bonc = info#springmvc视图解析器spring.mvc.view.prefix=/templates/spring.mvc.view.suffix=.html

后续还会写一些springBoot配置 国际化,读取配置文件,配置shiro,myBatis,redis等等一些的博客。
自己也在学习中,共勉。

原创不易-转载请注明出处。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。