欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot入门(web+freemarker)

程序员文章站 2023-12-14 14:41:34
1.配置maven文件pom.xml

1.配置maven文件pom.xml

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupid>com.hdwang</groupid>
  <artifactid>spring-boot-test</artifactid>
  <version>1.0-snapshot</version>
  <name>spring-boot-test</name>
  <description>project for test spring boot</description>
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <java.version>1.8</java.version>
  </properties>
  <!-- inherit defaults from spring boot -->
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.4.release</version>
    <relativepath/>
  </parent>
  <dependencies>
    <!-- add typical dependencies for a web application -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-freemarker</artifactid>
    </dependency>
    <!-- auto redeploy -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-devtools</artifactid>
      <optional>true</optional>
    </dependency>
  <!-- package as an executable jar -->
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

2.文件结构(static/templates/application.properties/logback.xml的名称都是约定好了的,只可以使用某几个名称,具体参考spring boot官方文档,下面的名称是其中一种配置方式)

Spring Boot入门(web+freemarker)

3.建立启动类(放在顶层,子层(下级文件夹)的类方可被扫描注入)

@springbootapplication
public class application {
  /**
   * main function
   * @param args params
   */
  public static void main(string[] args){
    springapplication.run(application.class,args);
  }
}

 4.建立controller(在application类的下级目录中)

@controller
@requestmapping("/common")
public class common {
  @value("${msg:welcome!}")
  private string msg;
  /**
   * get a page
   * @return a page with name called return value
   */
  @requestmapping("login")
  public string getloginpage(modelmap map){
    map.put("welcomemsg",this.msg);
    return "login";
  }
}

5.建立网页模板login.ftl(freemarker必须使用ftl后缀,被这个坑了好久!js/css啥的都放在相应文件夹下,注意访问路径中不带/static,也被这个坑了好久!)

<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
  <title>login</title>
  <link href="/css/home.css" rel="external nofollow" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="/js/jquery-2.0.3.min.js"></script>
  <script type="text/javascript" src="/js/home.js"></script>
</head>
<body>
<h1>login page</h1>
<h2>${welcomemsg}</h2>
<form>
  <div>
   <label>用户名:<input type="text" id="username"/></label>
  </div>
  <div>
   <label>密码:<input type="password"/></label>
  </div>
  <div>
    <input type="submit" value="提交"/>
    <input type="reset" value="重置" />
  </div>
</form>
</body>
</html>

6.应用配置文件编写

新建application.properties文件并添加以下内容

msg=ladies and gentleman,welcome!

7.启动运行

浏览器中访问:

Spring Boot入门(web+freemarker)

8.部署

mvn package 打个包

java -jar xxx.jar 运行这个包即可  

以上所述是小编给大家介绍的spring boot入门(web+freemarker),希望对大家有所帮助

上一篇:

下一篇: