使用Struts2(二)HelloWorld

来源:互联网 发布:中国联通通信网络维护 编辑:程序博客网 时间:2024/06/08 08:52

1.struts.xml

在上一篇的基础上加入新的action---名字是hello

<?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>/test01.jsp</result></action><action name="hello"class="com.yw.action.HelloWorldAction" method="execute"><result name="success">/HelloWorld.jsp</result></action></package></struts> 

2.编写model和action
package com.yw.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;}}
package com.yw.action;import com.opensymphony.xwork2.ActionSupport;import com.yw.model.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;}}


3.编写test02.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'test02.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1>Welcome To Struts 2!</h1><p><a href="<s:url action='hello'/>">Hello World</a></p></body></html>

4.编写HelloWorld.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %> <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'HelloWorld.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>        <h2><s:property value="messageStore.message" /></h2>   </body></html>

5.结构如图



6.运行


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 卷闸门钥匙丢了怎么办 邻居把路堵了该怎么办 邻居家的盆栽对准我家大门怎么办 袜子掉到了楼下的阳台怎么办? 合租房没有阳台晒衣服怎么办 车门锁了钥匙在里面怎么办 邻居忘带钥匙你看见会怎么办 把车钥匙锁车里了怎么办 偷了东西不承认怎么办 孩子偷了东西不承认怎么办 公司员工偷了东西不承认怎么办 知道被谁偷了没有证据怎么办 被贼偷了知道是谁没有证据怎么办 前任要去部队闹怎么办 站久了腿粗怎么办 机械表表带长了怎么办 恶魔之眼褪色了怎么办 恶魔之眼掉色后怎么办 练瑜伽出汗瑜伽垫手滑怎么办 cf多出来的神器怎么办 脖子痛得低不了怎么办 六岁的孩孑不吃.饭怎么办 大腿肌肉练废了怎么办 吃鸡里的信誉分太低怎么办she 血压高老是晕怎么办27 我腰疼的厉害怎么办 奥克斯空调外机上霜风扇不转怎么办 燃脂膏辣辣的怎么办 减肥期间暴食了怎么办 健身减脂后腹部皮松怎么办 吃减肥药上火怎么办呢 魔域怀旧版新区进不去怎么办 dnf十周年礼盒打开了怎么办 房地产项目完成后企业员工怎么办啊 韩服lol延迟太高怎么办 LOL等级奖励卡掉怎么办 魔域手机号换了怎么办 买的qq号找回了怎么办 买dnf账号被找回怎么办 微博账号已锁定怎么办 抖音账号封手机怎么办