目录:
- 概述
- 创建 model
- 创建 view
- 创建 controller
- 运行演示
- 演变修改
在 Struts2基础教程之一 中创建的web项目基础上,实现一个简单的Hello World的web应用。
[二]、创建 model
创建Model 类: MessageStore.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.micmiu.struts2.demo.helloworld.model; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class MessageStore { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } |
[三]、创建 view
创建页面:HelloWorld.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<%@ 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>Welcome to micmiu.com !</title> </head> <body> <h2> <s:property value="messageStore.message" /> </h2> </body> </html> |
[四]、创建 controller
创建Action 类:HelloWorldAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.micmiu.struts2.demo.helloworld.action; import com.micmiu.struts2.demo.helloworld.model.MessageStore; import com.opensymphony.xwork2.ActionSupport; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class HelloWorldAction extends ActionSupport { private static final long serialVersionUID = 1L; private MessageStore messageStore; public String execute() throws Exception { messageStore = new MessageStore(); messageStore.setMessage("Hello Struts2,welcome to www.micmiu.com !"); return SUCCESS; } public MessageStore getMessageStore() { return messageStore; } public void setMessageStore(MessageStore messageStore) { this.messageStore = messageStore; } } |
修改配置文件: struts.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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-struts2.jsp</result> </action> <action name="sayHello" class="com.micmiu.struts2.demo.helloworld.action.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts> |
[五]、运行演示
运行启动项目,打开浏览器输入:http://localhost:8082/struts2-basic-demo/sayHello.action 回车:
看到上述截图内容即表示运行成功了。
[六]、演变修改
简单演示struts2的标签以及Action的编码功能。
Model:MessageStore.java 增加的内容如下:
1 2 3 4 |
@Override public String toString() { return "MessageStore [message=" + message + "]"; } |
Action:HelloWorldAction.java 修改后如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.micmiu.struts2.demo.helloworld.action; import com.micmiu.struts2.demo.helloworld.model.MessageStore; import com.opensymphony.xwork2.ActionSupport; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class HelloWorldAction extends ActionSupport { private static final long serialVersionUID = 1L; private static int helloCount = 0; private String userName; private MessageStore messageStore; public String execute() throws Exception { messageStore = new MessageStore(); if (null != userName) { messageStore.setMessage("Hello " + userName + " ,welcome to www.micmiu.com"); } else { messageStore .setMessage("Hello Struts2,welcome to www.micmiu.com !"); } helloCount++; return SUCCESS; } public MessageStore getMessageStore() { return messageStore; } public void setMessageStore(MessageStore messageStore) { this.messageStore = messageStore; } public int getHelloCount() { return helloCount; } public void setHelloCount(int helloCount) { HelloWorldAction.helloCount = helloCount; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } } |
页面:index.jsp 修改成如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome to micmiu.com</title> </head> <body> <h1>Welcome To Struts2!</h1> <p> <a href="<s:url action='sayHello' />">Hello World</a> </p> <s:url action="sayHello" var="helloLink"> <s:param name="userName">Michael Sun</s:param> </s:url> <p> <a href="${helloLink}">Hello Michael Sun</a> </p> <s:form action="sayHello"> <s:textfield name="userName" label="Your name" /> <s:submit value="Submit" /> </s:form> </body> </html> |
页面:HelloWorld.jsp 修改成如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<%@ 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>Welcome to micmiu.com !</title> </head> <body> <p> I've said hello <s:property value="helloCount" /> times! </p> <h2> message:<s:property value="messageStore.message" /> </h2> <h2> toString:<s:property value="messageStore" /> </h2> </body> </html> |
运行启动项目,打开浏览器输入:http://localhost:8082/struts2-basic-demo/ 打开index.jsp
看到上述截图内容即表示运行成功了。
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/j2ee/struts/struts2-hello-world/
0 条评论。