无法使用参数获取QR码

时间:2018-02-01 03:22:09

标签: java spring spring-boot spring-data-jpa qr-code

我想生成QRcode并向页面显示一些数据,使用参数从数据库中获取数据。数据可以显示在页面上,但QRcode不显示。我希望QRCode只生成字段refNumber中的数据。请告诉我错误的

对于使用net.glxn qrgen 1.4 https://github.com/kenglxn/QRGen

的QRCode库

非常感谢你的帮助

这是我的控制器:

@RequestMapping(value="/result/{refNumber}", method = RequestMethod.GET)
    public String showEcustTemp ( @PathVariable("refNumber") String refNumber, Model model) {
        EcustTemp ecustTemp = ecustTempService.findByrefNumber(refNumber);
        byte[] bytes = QRCode.from(ecustTempService.findByrefNumber(refNumber)
                .toString()).withSize(120, 120).stream().toByteArray();
        model.addAttribute("qr", bytes);
        model.addAttribute("ecustTemp", ecustTemp);
        return "userFront/daftarResult";
    }

我的观点:

<img th:src="@{${qr.refNumber}}" />
<label for="idCard">ID CARD</label>: 
      <label th:text="${ecustTemp.idCard}"</label>
<label for="fullname">Full Name</label>: 
      <label th:text="${ecustTemp.fullName}"></label>
<label for="card">Card</label>: 
      <label th:text="${ecustTemp.typeCustomer}"></label>
<label for="address">address</label>: 
      <label th:text="${ecustTemp.address}"></label>
<label for="phone">phone</label>: 
      <label th:text="${ecustTemp.phone}"></label>
<label for="location">location</label>: 
      <label th:text="${ecustTemp.lapasLocation}"></label>

DAO班级:

public interface EcustTempDao extends CrudRepository<EcustTemp, Long>{
    EcustTemp findByrefNumber(String refNumber);

}

服务类:

public interface EcustTempService {
    void save (EcustTemp ecustTemp);
    EcustTemp createTempCustomer(EcustTemp ecustTemp);
    EcustTemp findByrefNumber(String refNumber);
}

服务实施类:

@Service
@Transactional
public class EcustTempServiceImpl implements EcustTempService   {

    @Autowired
    private EcustTempDao ecustTempDao;

    public void save(EcustTemp ecustTemp) {
        ecustTempDao.save(ecustTemp);
    }

    public EcustTemp createTempCustomer(EcustTemp ecustTemp) {
        ecustTemp.setRefNumber(UUID.randomUUID().toString());
        ecustTempDao.save(ecustTemp);
        return ecustTemp;
    }

    public EcustTemp findByrefNumber(String refNumber) {
        return ecustTempDao.findByrefNumber(refNumber);
    }

}

我的域名类:

@Entity
public class EcustTemp {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "ecustempId", nullable=false, updatable=false)
    private Long ecustempId;

    private String idCard;
    private String fullName;
    private String typeCustomer;
    private String pin;
    private String address;
    private String phone;
    private String lapasLocation;

    @Column(name = "refNumber", updatable=false)
    private String refNumber;
    private boolean active=false;

    @Column(name = "created_on")
    private Date createdOn;

    public Long getEcustempId() {
        return ecustempId;
    }
    public void setEcustempId(Long ecustempId) {
        this.ecustempId = ecustempId;
    }
    public String getIdCard() {
        return idCard;
    }
    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }
    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    public String getTypeCustomer() {
        return typeCustomer;
    }
    public void setTypeCustomer(String typeCustomer) {
        this.typeCustomer = typeCustomer;
    }
    public String getPin() {
        return pin;
    }
    public void setPin(String pin) {
        this.pin = pin;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getLapasLocation() {
        return lapasLocation;
    }
    public void setLapasLocation(String lapasLocation) {
        this.lapasLocation = lapasLocation;
    }

    public String getRefNumber() {
        return refNumber;
    }
    public void setRefNumber(String refNumber) {
        this.refNumber = refNumber;
    }
    public Date getCreatedOn() {
        return createdOn;
    }
    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }
    public boolean isActive() {
        return active;
    }
    public void setActive(boolean active) {
        this.active = active;
    }

错误:

There was an unexpected error (type=Internal Server Error, status=500).
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "qr.refNumber" (userFront/daftarResult:32)

1 个答案:

答案 0 :(得分:0)

此方法适用于我,我遵循此How to get QR code from id?

控制器:

@RequestMapping(value="/result/{refNumber}", method = RequestMethod.GET)
    public String showEcustTemp ( @PathVariable("refNumber") String refNumber, Model model) {
        EcustTemp ecustTemp = ecustTempService.findByrefNumber(refNumber);
        model.addAttribute("ecustTemp", ecustTemp);
        return "userFront/daftarResult";
    }

    @RequestMapping (value="/qr/{refNumber}", method = RequestMethod.GET)
    public HttpEntity<byte[]> getQRImage(@PathVariable String refNumber) {
        byte[] bytes = QRCode.from(ecustTempService.findByrefNumber(refNumber).getRefNumber()
                .toString()).withSize(120, 120).stream().toByteArray();
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity <byte[]> (bytes, headers, HttpStatus.CREATED);
    }

我的观点:

<img th:src="@{/qr/} + ${ecustTemp.refNumber}" />