用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
本文地址:[http://blog.csdn.net/sushengmiyan/article/details/40349201](http://blog.csdn.net/sushengmiyan/article/details/40349201) 官方文档:[ http://struts.apache.org/release/2.3.x/docs/hello-world-using-struts-2.html](http://struts.apache.org/release/2.3.x/docs/hello-world-using-struts-2.html)[](http://struts.apache.org/release/2.3.x/docs/hello-world-using-struts-2.html)[](http://docs.sencha.com/extjs/5.0/apidocs/#!/api/Ext-method-each) 本文作者:[sushengmiyan](http://blog.csdn.net/sushengmiyan) ------------------------------------------------------------------------------------------------------------------------------------ 当你在Struts 2 web应用中点击一个超链接或者提交一个form表单数据时,输入的数据并没有传到服务器的另一个页面,而是到了你提供的一个Java类. 这些类就叫做Actions. 当Action被fire, 结果会选择一个资源呈现响应. 通常情况下,资源是一个网页页面, 也可以是PDF文件, 或者是Excel页签, 或者是一个Java applet窗口. 假设你想创建一个简单的"Hello World" 例子展示欢迎信息. 在简单设定struts工作环境之后(参照 [如何创建struts 2 web应用](#)), 创建"Hello World"例子,你需要做如下四件事情: 1. 创建一个存储欢迎信息的java类(模型model) 1. 创建一个呈现信息的页面(视图 view) 1. 创建一个Action类来控制用户、模型和视图之间的关系(控制器controller) 1. 创建一个mapping (struts.xml) 来组合Action类和视图 这篇文章假定你已经完成了 怎样创建一个Struts 2 Web应用 体验并且拥有一个基础的struts工作空间. 这篇helloworld体验的源码,可以从Struts 2 GitHub 库https://github.com/apache/struts-examples下载. 例子工程使用maven管理依赖和构造.war文件. ### 第一步- 创建模型类 MessageStore.java 在我们第一个例子中,我们新建一个包com.susheng.struts2Maven.bean在里面新建一个类MessageStore,内容如下: ~~~ package com.susheng.struts2Maven.bean; public class MessageStore { private String message; public MessageStore() { setMessage("Hello Struts User"); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } ~~~ ![](https://box.kancloud.cn/2016-02-03_56b215a035494.jpg) ### 第二步 - 创建Action类 HelloWorldAction.java 新建包com.susheng.struts2Maven.action在此包下新建类HelloWorldAction,内容如下: ~~~ package com.susheng.struts2Maven.action; import com.opensymphony.xwork2.ActionSupport; import com.susheng.struts2Maven.bean.MessageStore; public class HelloWorldAction extends ActionSupport{ private static final long serialVersionUID = 1L; private MessageStore messageStore; public String execute() throws Exception { messageStore = new MessageStore() ; return SUCCESS; } public MessageStore getMessageStore() { return messageStore; } public void setMessageStore(MessageStore messageStore) { this.messageStore = messageStore; } } ~~~ ![](https://box.kancloud.cn/2016-02-03_56b215a05d12e.jpg) ### 第三步 - 创建View HelloWorld.jsp 内容如下: ~~~ <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Hello World!</title> </head> <body> <h2><s:property value="messageStore.message" /></h2> </body> </html> ~~~ ![](https://box.kancloud.cn/2016-02-03_56b215a08e5e1.jpg) ### 第四步 - 增加Struts配置到struts.xml 完整内容如下: ~~~ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="basicstruts2" extends="struts-default"> <action name="index"> <result>/index.jsp</result> </action> <action name="hello" class="com.susheng.struts2Maven.action.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts> ~~~ ![](https://box.kancloud.cn/2016-02-03_56b215a0b68c6.jpg) ### 第五步 - 创建URL Action index.jsp修改为如下: ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Basic Struts 2 Application - Welcome</title> </head> <body> <h1>Welcome To Struts 2!</h1> <p><a href="<s:url action='hello'/>">Hello World</a></p> </body> </html> ~~~ ![](https://box.kancloud.cn/2016-02-03_56b215a0dc683.jpg) ### 第六步 - 构造WAR文件并运行程序 我构造的war包是basic_struts.war所以我访问链接:http://localhost:8080/basic_struts/index.action,进入之后点击helloworld链接,正常跳转。 ![](https://box.kancloud.cn/2016-02-03_56b215a111861.jpg) 第二篇实际例子就学习完毕。 struts2的工作原理: ![](https://box.kancloud.cn/2016-02-03_56b215a142933.jpg)