GVKun编程网logo

Struts 2 学习笔记 (一) HelloWorld(struts2入门)

16

以上就是给各位分享Struts2学习笔记(一)HelloWorld,其中也会对struts2入门进行解释,同时本文还将给你拓展bootstrap(1)Helloworld、C#学习笔记之-----使用

以上就是给各位分享Struts 2 学习笔记 (一) HelloWorld,其中也会对struts2入门进行解释,同时本文还将给你拓展bootstrap(1) Helloworld、C#学习笔记之-----使用委托来用不同语言写HelloWorld、C++ 学习笔记(HelloWorld, 类型和值)、CXF 学习笔记 (1)-HelloWorld!- 发布 webservice等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Struts 2 学习笔记 (一) HelloWorld(struts2入门)

Struts 2 学习笔记 (一) HelloWorld(struts2入门)

 

1.       什么是 struts2?

Struts2 是一个 mvc 框架。

Apache Struts is a free, open-source, MVC framework for creating elegant, modern Java web applications. It favors convention over configuration, is extensible using a plugin architecture, and ships with plugins to support REST, AJAX and JSON.

2.       Struts2=struts1+webwork。

3.       在 struts2 之前就已经有很多 mvc 框架。当时 Strtus1 是主流的 mvc 框架。随着技术发展,struts1 不支持新的表示层技术。Strtus1 和 webwork 整合而来 struts2。

Struts2 的核心是 webwork, 是一个轻量级的 mvc 框架,是一个基于请求的 mvc 框架。

4.       轻量级框架一般占用资源较少,使用比较方便,不具有侵入性的。

5.       没有侵入性指使用一个框架,不去继承或者实现框架提供的类或接口。

6.       使用 struts2 开发一个 helloworld 程序

a)         新建 web 项目

b)        添加 index.jsp 页面

c)         添加 struts2 需要的 jar 包

asm-x.x.jar

asm-commons-x.x.jar

asm-tree-x.x.jar

commons-fileupload-X.X.X.jar

commons-io-X.X.X.jar

commons-lang-X.X.jar

commons-lang3-3.1.jar

freemarker-X.X.X.jar

javassist-X.X.X.jar

ognl-X.X.X.jar

struts2-core-X.X.X.X.jar

xwork-core-X.X.X.jar   (最新的 2.5.10.1 将 xwork 包与 struts 包合并了)

 

d)        在 web.xml 中添加 struts2 的核心过滤器

<!-- struts2 的核心过滤器 -->

  <filter>

  <filter-name>struts2</filter-name>

  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

  </filter>

  <filter-mapping>

  <filter-name>struts2</filter-name>

  <url-pattern>/*</url-pattern>

  </filter-mapping>

 

e)         新建一个 Action 类,该类用于处理请求

public class HelloAction {

 

   public String execute(){

      System.out.println ("处理 action 的请求");

      return "index";

   }

}

 

f)         在 src 下创建 struts.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

   "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

   <constant name="struts.devMode" value="true" />

   <package name="default" extends="struts-default">

      <action name="hello" class="cn.sxt.action.HelloAction">

         <result name="index">/index.jsp</result>

      </action>

   </package>

</struts>

 

g)        测试:http://localhost/helloworld/hello

 

bootstrap(1) Helloworld

bootstrap(1) Helloworld

总结

以上是小编为你收集整理的bootstrap(1) Helloworld全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

C#学习笔记之-----使用委托来用不同语言写HelloWorld

C#学习笔记之-----使用委托来用不同语言写HelloWorld

最近学到委托了,贴上代码吧。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Delegate
{
    public delegate void GreetingDelegate (string name);
    class Program
    {
        public static void MakeGreeting (string name,GreetingDelegate gd)
        {
            gd(name);
        }

        public static void EnglishGreeting(string name)
        {
            Console.Write("Hello," + name + "\n");
        }

        public static void ChineseGreeting(string name)
        {
            Console.Write("你好," + name + "\n");
        }
        static void Main(string[] args)
        {
            MakeGreeting("World", EnglishGreeting);
            MakeGreeting("世界", ChineseGreeting);
            Console.ReadKey();
        }
    }
}

推荐扩展阅读:

百度百科-----C#委托

Wikipedia-----委托模式

让我们一同学习C#,一同进步!


C++ 学习笔记(HelloWorld, 类型和值)

C++ 学习笔记(HelloWorld, 类型和值)

现在有一个从控制台读取输入的小程序:

 1 #include  "../std_lib_facilities.h"
 2 
 3 int main() {
 4 
 5   cout << "Please enter your first name and age\n";
 6   string first_name;
 7   int age;
 8   cin >> first_name;
 9   cin >> age;
10   cout << "Hello, " << first_name << " (age " << age << ")\n";
11 
12   return 0;
13 }

 

如果你输入了 "Carlos 22",>> 操作符会将 Carlos 读入 first_name,将 22 读入 age。为什么它不将 "Carlos 22" 全部读入 first_name 呢?这是由于按照规定,读取字符串会被空白符所终止,包括空格、换行和 tab 字符。除此之外,空格会在缺省的情况下被 >> 忽略。如果你输入的是 "22 Carlos",你将会看到奇怪的东西。22 按照字符串进行读取,但是 Carlo 因为不是一个整数,因此它不会被读取,age 没有被赋一个初始值,而是内存中的一个垃圾值。<<和>> 都是对类型敏感的。

关于用 C++ 中从控制台读取带空格的字符串可以自行查找。

 

CXF 学习笔记 (1)-HelloWorld!- 发布 webservice

CXF 学习笔记 (1)-HelloWorld!- 发布 webservice

1.apache 网站下载 CXF http://cxf.apache.org/download.html

2. 创建一个 java 工程,将以下 jar 包复制到工程的 classpath 下

所有的 jar 包都可以在 ${CXF_HOME}\lib 目录中找到

3. 定义服务接口 HelloWorldService

因为这个接口将会被我们暴露为 webservice, 所以给该接口加一个 @WebService 标注

package com.crazycoder2010.webservice.cxf.server;

import javax.jws.WebParam;
import javax.jws.WebService;


/**
 * 服务器端对外提供的服务
 * @author Kevin_Wang03
 *
 */
@WebService
public interface HelloWorldService {
	/**
	 * 简单的字符串参数
	 * @param userName
	 * @return
	 */
	public String sayHello(@WebParam(name="userName") String userName);
	
	/**
	 * 参数为对象的情况
	 * @param user
	 * @return
	 */
	public String sayHelloToUser(User user);
}

4. 提供具体的 webservice 提供者 HelloWorldServiceImpl

这个实现类实现了我们上面的服务接口,除了要添加 @WebService 标注外,还要定义该服务的名称 serviceName="helloWorldService" 和 endpoint (服务接口),其他和普通类没有任何区别

package com.crazycoder2010.webservice.cxf.server;

import javax.jws.WebService;

/**
 * 默认的webservice实现
 * 
 * @author Kevin_Wang03
 * 
 */
@WebService(endpointInterface = "com.crazycoder2010.webservice.cxf.server.HelloWorldService", serviceName = "helloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {

	@Override
	public String sayHello(String userName) {
		System.out.println("HelloWorldServiceImpl.sayHello("+userName+")");
		return "Hello,"+userName;
	}

	@Override
	public String sayHelloToUser(User user) {
		System.out.println("HelloWorldServiceImpl.sayHelloToUser("+user+")");
		return "Hello,("+user.getId()+","+user.getName()+")";
	}
}

5. 通过 JAX-WS 将类发布为服务

这个是个普通类,要发布一个 webservice,首先要定义好这个 webservice 的访问地址和端口,然后需要知道把哪个类发布成服务,就这么简单?yes!

package com.crazycoder2010.webservice.cxf.server;

import javax.xml.ws.Endpoint;

public class Server {
	public static void main(String[] args) {
		System.out.println("Starting Server");
		HelloWorldServiceImpl helloWorldServiceImpl = new HelloWorldServiceImpl();
		String address = "http://localhost:9000/helloWorldService";
		Endpoint.publish(address, helloWorldServiceImpl);
		System.out.println("Start success");
	}
}

6. 通过 URL 访问 WSDL 看看是否显示正常

http://localhost:9000/helloWorldService?wsdl

分析输出的 wsdl 文件

6.1<wsdl:definitions name="helloWorldService" >

这个 name 就是我们在 HelloWorldServiceImpl 类的标注 serviceName="helloWorldService" 生成的

6.2<wsdl:definitions targetNamespace="http://server.cxf.webservice.crazycoder2010.com/">

注意到我们的代码位于 com.crazycoder2010.webservice.cxf.server 中,记得 sun 推荐的包的命名标准吗?就是公司域名翻转加上项目名,那把包名翻过来是什么?- 你懂得,CXF 就用了这种机制

6.3<xs:complexType name="sayHello">

这个即是我们定义在服务接口中的方法,在 wsdl 文件中将接口中的方法定义成服务方法,里面的子元素定义了该方法的参数

6.4<xs:complexType name="sayHelloResponse">

这个是对接口服务方法返回值的描述,默认遵循以下约定对方法的描述为 sayHello,则返回值的描述为 sayHelloResponse

6.5<wsdl:message name="sayHelloToUser">

这个描述把服务和方法绑定起来,与 Response 是成对出现的

6.6<soap:address location="http://localhost:9000/helloWorldService"/>

这里就是我们上面在代码里完成的服务的访问地址

<?xml version="1.0" ?><wsdl:definitions name="helloWorldService" targetNamespace="http://server.cxf.webservice.crazycoder2010.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.cxf.webservice.crazycoder2010.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:types>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://server.cxf.webservice.crazycoder2010.com/" xmlns="http://server.cxf.webservice.crazycoder2010.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="user">
    <xs:sequence>
      <xs:element name="id" type="xs:int"></xs:element>
      <xs:element minOccurs="0" name="name" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="sayHelloToUser" type="sayHelloToUser"></xs:element>

  <xs:complexType name="sayHelloToUser">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="user"></xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="sayHelloToUserResponse" type="sayHelloToUserResponse"></xs:element>
  <xs:complexType name="sayHelloToUserResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"></xs:element>

    </xs:sequence>
  </xs:complexType>
  <xs:element name="sayHello" type="sayHello"></xs:element>
  <xs:complexType name="sayHello">
    <xs:sequence>
      <xs:element minOccurs="0" name="userName" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="sayHelloResponse" type="sayHelloResponse"></xs:element>

  <xs:complexType name="sayHelloResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
  </wsdl:types>
  <wsdl:message name="sayHelloToUser">
    <wsdl:part element="tns:sayHelloToUser" name="parameters">

    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sayHelloToUserResponse">
    <wsdl:part element="tns:sayHelloToUserResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sayHelloResponse">
    <wsdl:part element="tns:sayHelloResponse" name="parameters">
    </wsdl:part>

  </wsdl:message>
  <wsdl:message name="sayHello">
    <wsdl:part element="tns:sayHello" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="HelloWorldService">
    <wsdl:operation name="sayHelloToUser">
      <wsdl:input message="tns:sayHelloToUser" name="sayHelloToUser">
    </wsdl:input>

      <wsdl:output message="tns:sayHelloToUserResponse" name="sayHelloToUserResponse">
    </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="sayHello">
      <wsdl:input message="tns:sayHello" name="sayHello">
    </wsdl:input>
      <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
    </wsdl:output>
    </wsdl:operation>

  </wsdl:portType>
  <wsdl:binding name="helloWorldServiceSoapBinding" type="tns:HelloWorldService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
    <wsdl:operation name="sayHelloToUser">
      <soap:operation soapAction="" style="document"></soap:operation>
      <wsdl:input name="sayHelloToUser">
        <soap:body use="literal"></soap:body>
      </wsdl:input>
      <wsdl:output name="sayHelloToUserResponse">

        <soap:body use="literal"></soap:body>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document"></soap:operation>
      <wsdl:input name="sayHello">
        <soap:body use="literal"></soap:body>
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">

        <soap:body use="literal"></soap:body>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="helloWorldService">
    <wsdl:port binding="tns:helloWorldServiceSoapBinding" name="HelloWorldServiceImplPort">
      <soap:address location="http://localhost:9000/helloWorldService"></soap:address>
    </wsdl:port>
  </wsdl:service>

</wsdl:definitions>

小结:

HelloWorld 是跑起来了,感觉比先前用的 JAX-RPC 要简单的多了,几分钟就可以搞定一个 webservice-- 也许这就是框架的魅力,但是也有一些疑问,如要运行这么小一个 helloWorld,要依赖 20 几个 jar 包,其中还有 Jetty 的 jar 包若干!竟然还有 org.eclipse.jetty.* 这种包,不知道设计这个框架的同学是怎么考虑的,为什么要把 webservice 和 jetty 或 eclipse 扯上关系,可能看的不够深,明天继续研究。

原文转自:http://blog.csdn.net/crazycoder2010/article/details/6673269

关于Struts 2 学习笔记 (一) HelloWorldstruts2入门的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于bootstrap(1) Helloworld、C#学习笔记之-----使用委托来用不同语言写HelloWorld、C++ 学习笔记(HelloWorld, 类型和值)、CXF 学习笔记 (1)-HelloWorld!- 发布 webservice等相关内容,可以在本站寻找。

本文标签: