MyEclipse 10 下的Struts 2 的开发范例(一)

Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 1 页
MyEclipse 10 下的
Struts 2 开发范例
(一)
作者:Steel.Ma
Mail: steel.ma@139.com
MSN: Motorola_8088@hotmail.com
Blog: http://blog.tom.com/steel.ma
微博: http://weibo.com/iamsteelma
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 2 页
目录
一、环境说明 .................................................... 3
二、制作内容 .................................................... 4
1、新建一个Web Service Project,并加入Struts 库 ............. 4
2、制作一个录入界面.......................................... 7
3、制作一个action ........................................... 9
3、制作一个结果显示页面..................................... 10
4、装配jsp、action 与result ................................ 11
三、测试 ....................................................... 12
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 3 页
一、环境说明
MyEclipse 10 是新发布的一个版本,不过根据我的使用,感觉没有以前的版
本稳定,颇感失望。
JBoss 4.3 是我一直使用的一个版本。
(安装、配置略)
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 4 页
二、制作内容
1、新建一个Web Service Project,并加入Struts 库
下一个画面取默认后,project 文件生成。
选择新生成的project,然后菜单:MyEclipse-> Project Capabilities-> Add
Struts Capabilities
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 5 页
然后选择Struts 2.1
Next 之后,选择默认默认的Struts Core 库后完成:
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 6 页
以上操作完成后,本项目就具备了struts 2 的类库,同时核心的web.xml
文件也自动进行了配置。可以查看该文件确认:
<?xml version="1.0" encoding="UTF‐8"?>
<web‐app version="2.5"
xmlns=" http://java.sun.com/xml/ns/javaee"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema‐instance"
xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web‐app_2_5.xsd">
<display‐name></display‐name>
<welcome‐file‐list>
<welcome‐file>index.jsp</welcome‐file>
</welcome‐file‐list>
<filter>
<filter‐name>struts2</filter‐name>
<filter‐class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter‐class>
</filter>
<filter‐mapping>
<filter‐name>struts2</filter‐name>
<url‐pattern>/*</url‐pattern>
</filter‐mapping></web‐app>
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 7 页
2、制作一个录入界面
先在src 目录下建立一个package,包名为com.steelma.struts.action
在webroot 下,新增一个user.jsp 文件,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf‐8"%>
<%
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 'user.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">
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 8 页
<meta http‐equiv="description" content="This is my page">
<!‐‐
<link rel="stylesheet" type="text/css" href="styles.css">
‐‐>
</head>
<body>&nbsp;
<br>
<form method="post" action="newuser.action" name="createuser"><table
border="0">
<tbody><tr>
<td>&nbsp;用户名:</td>
<td>&nbsp;<input type="text" name="username"></td>
<td>&nbsp;</td></tr>
<tr>
<td>&nbsp;密码:</td>
<td>&nbsp;<input type="password" name="password"></td>
<td>&nbsp;</td></tr>
<tr>
<td>&nbsp;邮箱:</td>
<td>&nbsp;<input type="text" name="mailbox"></td>
<td>&nbsp;</td></tr>
<tr>
<td>&nbsp;年龄</td>
<td>&nbsp;<input type="text" name="age"></td>
<td>&nbsp;</td></tr>
</tbody></table><p>&nbsp;<input type="submit" value="确定"
name="submit"></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p></for
m></body>
</html>
请注意其中底色标注为黄色的代码:
<form method="post" action="newuser.action" name="createuser"><table
border="0">
中,action="newuser.action" 表示,本form 将提交给名为newuser 的struts
action 进行处理,而余下4 个则是将提交给action 的参数,其的名字必须与
action 中一致。
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 9 页
3、制作一个action
在src/com.steelma.struts.action 包下,创建一个名为newuser 的class,
代码如下:
package com.steelma.struts.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class newuser extends ActionSupport{
private String username ;
private String password ;
private String mailbox ;
private int age ;
public String getUsername() {
return this.username ;
}
public String getPassword() {
return this.password ;
}
public String getMailbox() {
return this.mailbox ;
}
public int getAge() {
return this.age ;
}
public void setUsername(String username) {
this.username = username ;
}
public void setPassword(String password) {
this.password = password ;
}
public void setMailbox(String mailbox) {
this.mailbox = mailbox ;
}
public void setAge(int age) {
this.age = age ;
}
public String execute() throws Exception {
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 10 页
System.out.print("ok ! " + getUsername()) ;
// 这里可以把接受到的数据写入数据库中
return SUCCESS;
}
}
说明:
Struts 2 中有2 种action 驱动模式(也就是与前端交互的方法),一种是
model Driven,简单的说,也就是要单独建一个Java Bean 作为model 来承载,
不过这种方式比较繁琐,因此,作为一个懒人,笔者比较喜欢property Driven
方式,也就直接将model 的字段及其getter 和setter 方法直接建立在action
中,这样就不用单独建立form bean 和做出配置了,一般来说,只要form 的字
段内容不是太复杂,建议采用这种方法。
所以分析上述的代码,可以看出action 中的各个字段均与jsp 中的内容对
应。
3、制作一个结果显示页面
Action 运行完毕后,会自动返回一个success,我们制作一个将录入的值显
示的页面。
在webroot 下建立一个userok.jsp,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf‐8"%>
<%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort
()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts‐tags" %>
<!DOCTYPE HTML PUBLIC "‐//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 11 页
<title>My JSP 'userok.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>
用户名=<s:property value="username"/> <br>
密码=<s:property value="password"/> <br>
邮箱=<s:property value="mailbox"/> <br>
年龄=<s:property value="age"/> <br>
</body>
</html>
说明:
<%@ taglib prefix="s" uri="/struts‐tags" %>
是引入struts 的tag lib,这是struts 2 的重要改进,非常方便
4、装配jsp、action 与result
装配的核心文件就是src 下的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>
<package name="default" namespace="/" extends="struts‐default">
<action name="newuser" class="com.steelma.struts.action.newuser">
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 12 页
<result>/userok.jsp</result>
</action>
<!‐‐ Add actions here ‐‐>
</package>
<!‐‐ Add packages here ‐‐>
</struts>
三、测试
使用MyEclipse 将代码发布到JBoss 中,并启动JBoss 服务器,然后打开浏
览区,输入URL:
http://localhost:8080/struts2/user.jsp
在画面上录入内容,结果如下:
按确定提交后,系统会自动启动action,进行参数接收,并返回到显示jsp 上:
Steel.Ma 开发笔记MyEclipse 10 下的Struts 2 的开发范例(一)
第 13 页
同时,MyEclipse 的console 上也打印了调试信息:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值