有了上一节的成功尝试,我明显感觉信心倍增,自信满满,事不宜迟,继续加油咯
这一次,是动手实验篇,我偏重于在教程中找到重点信息,摘录备忘,以及记录整个实现过程;单词都是很简单的,加油
学习地址:http://struts.apache.org/2.2.1/docs/hello-world-using-struts-2.html
实现目标:Suppose you want to create a simple "Hello World" example that displays a welcome message.
1.Create a class to store the welcome message (the model)
2.Create a server page to present the message (the view)
3.Create an Action class to control the interaction between the user, the model, and the view (the controller)
4.Create a mapping (struts.xml) to couple the Action class and view
Step 1 - Create The Model Class MessageStore.java
create the MessageStore class in src/main/java. Be sure to note the package statement below.(在项目目录下的src/main/目录下建立java文件夹,并且确保java文件的包结构正确)
package org.apache.struts.helloworld.model; 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; } }
In the model class above note the use of public set and get methods to allow access to the private message String attribute.
Step 2 - Create The Action Class HelloWorldAction.java
One or more of the Action class's methods are executed and a String result is returned. Based on the value of the result, a specific view page (in this example that view page is HelloWorld.jsp) is rendered.
package org.apache.struts.helloworld.action; import org.apache.struts.helloworld.model.MessageStore; import com.opensymphony.xwork2.ActionSupport; 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; } }
Step 3 - Create The View HelloWorld.jsp
Create the below jsp in src/main/webapp for the project.
<%@ 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>
Step 4 - Add The Struts Configuration In struts.xml
Edit the struts.xml file ( in the project that file is in the src/main/resources folder) to add the action mapping.
Place the action node (action name="hello") between the opening and closing package node, just after the action mapping with the name="index". Your complete struts.xml should look like:
<?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="org.apache.struts.helloworld.action.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
Step 5 - Create The URL Action
In index.jsp (see WebContent folder for Ant project and src/main/webapp for Mvn project) let's add an Action URL the user can click on to tell the Struts 2 framework to run the execute method of the HelloWorldAction class and render the HelloWorld.jsp view.
First add the taglib directive at the top of the jsp <%@ taglib prefix="s" uri="/struts-tags" %>. Next add this p tag <p><a href="<s:url action='hello'/>">Hello World</a></p> after the h1 tag. Your newindex.jsp should look like:
<%@ 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>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>
The Struts url tag creates the URL with an action of hello. The hello action was mapped to the HelloWorldAction class and its execute method. When the user clicks on the above URL it will cause the Struts 2 framework to run the execute method of the HelloWorldAction class. After that method returns the String success, the view page HelloWorld.jsp will be rendered.
Step 6 - Build the WAR File and Run The Application
web容器中运行正常就成功咯
相关推荐
struts2的概述struts2下的HelloWord struts2基础 struts2做好准备 struts2的标志 struts2的Action讲解 struts2中的国际化 struts2转化器 struts2实现表单数据校验 struts2的基石-拦截器 struts2中实现IOC struts2中...
struts2标签使用例子 struts2标签使用例子 struts2标签使用例子struts2标签使用例子
这是学习struts2时记得重点笔记,包括了一些原理,ognl语句的编写,以及如何设置拦截器等等一些基本知识,起到复习和巩固的作用
struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别...
Struts2是一套非常优秀的Java WEB应用框架,实现优雅、功能强大、使用简洁。目前已有大量的企业项目采用Struts2来作为Web框架进行开发,掌握Struts2是每个进行Web开发的Java程序员的一项必备技能。 本视频对Struts2...
struts2学习测试代码,struts2学习测试代码2struts2学习测试代码,struts2学习测试代码
Struts 2是Struts的下一代产品,是在 struts 和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构的差别巨大。Struts2以WebWork为核心,采用拦截器的机制来处理...
本人在网上收集的struts2 的学习资料,包括:struts2权威指南、struts2标签简单说明、struts2中文教程。
struts2的根本webwork2
大三学习struts2后纯struts2做的通讯录 数据库源码统统唷
struts2源码 struts2案例 struts2使用包
struts4 使用Struts2中内部类中的方法进行自定义类型转换,用到的类StrutsTypeConverter struts5 增加集合类型,使用范型 struts6 直接设置要进行类型转换的字段值 struts7 制作一个表单,对表单中的内容进行...
STRUTS2站点,STRUTS2学习实例8
关于Struts2的使用详情,以及绝对路径和相对路径的使用
用struts2 doubleselect标签实现的二级联动下拉菜单
使用struts2开发之前必须引入struts的包。
struts2使用手册
javaEE学习阶段,struts2初步使用环境配置。 javaEE学习阶段,struts2初步使用环境配置。
struts2 资料struts2 资料struts2 资料struts2 资料