solidity入门1. HelloWeb3

Posted JLDS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了solidity入门1. HelloWeb3相关的知识,希望对你有一定的参考价值。

Solidity简述

solidity是以太坊虚拟机(EVM)智能合约的语言

区块链项目大部分是开源的,如果能读懂代码,就可以规避很多亏钱的项目

开发工具remix

remix是以太坊官方推荐的智能合约开发IDE(集成开发环境),适合新手,可以在浏览器中快速部署测试智能合约,不需要在本地安装任何程序

左边菜单:文件—写代码;编译—跑代码;部署—部署到链上

点击create new file,可以创建空白的solidity合约

网址:remix.ethereum.org (需要梯子)

第一个程序

// SPDX-License-Identifier: MIT
prama solidity ^0.8.4;
contract HelloWeb3
    string public _string = "HelloWeb3!";

第一行注释:”//“开头

写代码所用的软件许可(license),这里用的是MIT license

如果不写许可,编译时会被警告(warning),但程序可以运行

“//”后的内容不会被程序运行

第二行声明源文件所用的solidity版本

不同版本语法有差别

本行代码意思是:源文件将不允许小于0.8.4版本或大于等于0.9.0版本的编译器编译(第二个条件由^提供)

solidity语句以分号 ; 结尾

三、四行是合约部分

第三行创建合约(contract):声明合约名字HelloWeb3

第四行是合约内容,声明了一个string(字符串)变量_string,并赋值为”HelloWeb3!”

编译&部署代码

在编辑界面,ctrl+S即可编译(跑代码)

编号后点击“部署Deploy”(到链上),进入部署界面

默认情况下 remix会用JS虚拟机模拟以太坊链,运用智能合约,类似在浏览器里跑一条测试链

且remix会分配几个测试账户,每个里面有100ETH(测试代币)

部署后就可以看到名为HelloWeb3的合约

再点击_string,就可以看到代码中的”Hello Web3!”

No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb

I‘m learning the Spring Framework, and I‘m doing the HelloWeb tutorial on tutorialspoint, and I can‘t get it working. I‘m using Spring MVC 4.0, and I‘m deploying my app from Netbeans 8.0 to a Glassfish Server. http://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm

I looked for similar problems here on this site and on other sites as well, but the suggested solutions didn‘t work for me. I would really appreciate some help, because I‘m pretty sure I‘m missing something basic here.

Here are my relevant files:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">

<display-name>Spring MVC Application</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
</web-app>

HelloWeb-servlet.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans     
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<context:component-scan base-package="com.tutorialspoint" />

<bean id="viewResolver"  class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

HelloController.java

package com.tutorialspoint;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

You have a single controller in your app, mapped to /hello (that‘s what @RequestMapping("/hello")means). The URL for that controller is thus http://localhost:8080/HelloWeb/hello.

 

以上是关于solidity入门1. HelloWeb3的主要内容,如果未能解决你的问题,请参考以下文章

Solidity零基础入门Solidity编写智能合约代码

Solidity零基础入门Solidity编写智能合约代码

Solidity零基础入门Solidity编写智能合约代码

Solidity零基础入门Solidity编写智能合约代码

Solidity入门

智能合约从入门到精通:Solidity汇编语言