跟着官网学Struts2使用(二)
来源:互联网 发布:舞台制作软件 编辑:程序博客网 时间:2024/06/05 10:17
有了上一节的成功尝试,我明显感觉信心倍增,自信满满,事不宜迟,继续加油咯
这一次,是动手实验篇,我偏重于在教程中找到重点信息,摘录备忘,以及记录整个实现过程;单词都是很简单的,加油
学习地址: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使用(一)
- 使用Struts2(二)HelloWorld
- 跟着沈毅学微电商(PHP):二
- 跟着官网学solr(二):Document、Field说明
- 跟着实例学习OpenLayers(二)
- 跟着某乎玩python(二)
- Struts2(二)使用Struts2访问ServletAPI对象
- Struts2学习(二)--使用struts2编写helloworld
- Struts2(二)使用Struts2访问ServletAPI对象
- struts2中使用ajax之二
- struts2拦截器的使用(二)
- struts2系列二之namespace 的使用
- 【实习设计二】为什么使用Struts2
- Struts2核心(二)----使用OGNL表达式
- Struts2框架的基本使用(二)
- 跟着thinkphp官方论坛学TP 二
- 跟着鬼哥学android java hook(二)
- 自己动手写操作系统
- Superprime Rib
- 网站运营决胜秘籍的三个要点
- 深入浅出 CPropertySheet
- 韩顺平_php从入门到精通_视频教程_学习笔记_源代码图解_PPT文档整理_目录
- 跟着官网学Struts2使用(二)
- 三基站定位演示网址
- 易虎再谈网站被恶意刷流量和防恶意点击的解决思路
- 浅析三点SEO理论到底带给了我们什么
- SAP 技术理解-记录
- Mysql插入数据错误
- 做好这六点 网站推广不在难
- C++ 二进制位运算判断奇数偶数
- JUC类关系图