发表于: 2017-08-02 16:09:17

5 1005


今天做的事:

一个命令

mvn clean install -Dmaven.test.skip=ture

maven项目打包跳过单元测试


一个对象

json对象(JavaScript Object Notation)

一种数据传输约定

语法:

名称键值对数据表示——key:value

大括号持有对象和每个名称后跟:冒号,名称键值对的分离用,逗号

方括号持有数组和值

实例

{

    "book": [

    {

       "id":"01",

       "language": "Java",

       "edition": "third",

       "author": "Herbert Schildt"

    },

    {

       "id":"07",

       "language": "C++",

       "edition": "second"

       "author": "E.Balagurusamy"

    }]

}

一个数组book,其中包含两个book对象,每个对象中包含资格名称键值对


两个报错

maven项目出现GBK编码错误,解决如下

在pom文件中添加声配置

<!-- 指明编译源代码时使用的字符编码,maven编译的时候默认使用的GBK编码, 通过project.build.sourceEncoding属性设置字符编码,告诉maven这个项目使用UTF-8来编译 -->  

 <properties>  

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  

 </properties>  

或者添加插件

<plugin>  

    <artifactId>maven-compiler-plugin</artifactId>  

    <configuration>  

        <source>1.6</source>  

        <target>1.6</target>  

        <!-- 指明编译源代码时使用的字符编码,  

        maven编译的时候默认使用的GBK编码,   

          通过encoding属性设置字符编码,  

          告诉maven这个项目使用UTF-8来编译 -->  

        <encoding>utf8</encoding>   

    </configuration>  

</plugin>  


出现报错

-source 1.5 中不支持 diamond 运算符 [ERROR]   (请使用 -source 7 或更高版本以启用 diamond 运算符)

解决方案:

添加插件

<plugin>  

   <groupId>org.apache.maven.plugins</groupId>  

    <artifactId>maven-compiler-plugin</artifactId>  

     <version>3.6.1</version>  

   <configuration>  

     <source>1.7</source>  

        <target>1.7</target>  

      <encoding>UTF-8</encoding>  

    </configuration>  

</plugin>  


使用jetty插件启动服务

<plugins>
 <plugin>
   <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-maven-plugin</artifactId>
   <version>9.3.7.v20160115</version>
   <configuration>
     <stopPort>9966</stopPort>
     <stopKey>foo</stopKey>
     <stopWait>10</stopWait>
   </configuration>
 </plugin>

 <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.6.1</version>
   <configuration>
     <source>1.7</source>
     <target>1.7</target>
     <encoding>UTF-8</encoding>
   </configuration>
 </plugin>

</plugins>


插件装好后,使用mvn jetty:run运行服务


接口测试完毕,差一个jsp页面还未写完,然后还有一点配置文件没弄完,明天在评论下补上


贴一下代码

BookController.java

package com.ts.web;

import com.ts.dto.AppointExecution;
import com.ts.dto.Result;
import com.ts.entities.Book;
import com.ts.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by Administrator on 2017/08/01.
*/
@Controller
@RequestMapping("/book")
public class BookController {

@Autowired
   private BookService bookService;

   @RequestMapping(value = "/list",method = RequestMethod.GET)
public String list(Model model){
List<Book> list = bookService.getList();
       for(Book book : list){
System.out.println(book);
       }
model.addAttribute("list",list);
       //list.jsp + model =ModelAndView
       return "list";//WEB_INF/jsp/"list".jsp
   }


//ajax json
   @RequestMapping(value = "/{bookId}/detail",method = RequestMethod.GET)

//异步获取json数据,加上@Responsebody后,会直接返回json数据。
   @ResponseBody
   public String detail(@PathVariable("bookId") Long bookId,Model model){
if(bookId == null){
return "redirect:/book/list";
       }
Book book = bookService.getById(bookId);
       if(book == null){
return "forward:/book/list";
       }
System.out.println(book);
       model.addAttribute("book",book);
       return "detail";
   }


//application/json是Content-Type,响应头,传输的主体是JSON字符串
   @RequestMapping(value = "/{bookId}/appoint",method = RequestMethod.POST,produces = {"application/json;charset=utf-8"})

//mybatis使用@Param注解时,在配置文件中可以使用${}的格式,如果没有该注解,必须使用#{}
   public Result appoint(@PathVariable("bookId") Long bookId, @Param("studentId") Long studentId){
if(studentId == null||studentId.equals("")){
return new Result<>(false,"学号不能为空");
       }
AppointExecution execution = bookService.appoint(bookId,studentId);
       System.out.println(execution);
       return new Result(true,execution);
   }

}


配置文件

spring-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


   <!-- 扫描service包下所有使用注解的类型 -->
   <context:component-scan base-package="com.ts.web"/>

       <!-- 配置SpringMVC -->
       <!-- 1.开启SpringMVC注解模式 -->
       <!-- 简化配置:
           (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
           (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
       -->
       <mvc:annotation-driven/>

           <!-- 2.静态资源默认servlet配置
               (1)加入对静态资源的处理:js,gif,png
               (2)允许使用"/"做整体映射
            -->
       <mvc:default-servlet-handler/>

   <!-- 启用spring mvc 注解 -->
   <context:annotation-config/>

               <!-- 3.配置jsp 显示ViewResolver -->
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
           <property name="prefix">
               <value>/WEB-INF/jsp/</value>
           </property>
           <property name="suffix">
               <value>.jsp</value>
           </property>
       </bean>


</beans>


然后是

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="true" version="3.1"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee
                   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">


 <filter>
   <filter-name>HiddenHttpMethodFilter</filter-name>
   <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 </filter>
 <filter-mapping>
   <filter-name>HiddenHttpMethodFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

 <servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:spring/spring-*.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>


至此springMVC的整合也结束了。


jsp页面就不贴了,写的太丑


明天计划:做小课堂ppt;然后将jsp页面写完,将配置文件捋一捋;整合一下任务一的代码。


问题:暂无


收获:jsp页面;解决一点小bug;然后使用jetty插件启动服务。


返回列表 返回列表
评论

    分享到