Struts2的Helloworld(版本:2.0.14)
来源:互联网 发布:美国十次啦最新域名 编辑:程序博客网 时间:2024/05/11 19:34
主要参考马召博客和李刚《Struts2权威指南》
1、到Apache网站上下载Struts2,文中版本为2.0.14。解压缩。
2、在myeclipse中建立新的Web project,命名为Struts2Hello,Java specification level选Java EE5.0。建成后目录结构如下:
WEB-INF下还有个很重要的目录,classes
3、工程是普通的web工程,现在加入Struts2支持。
第一步,把必须的jar包拷贝到WEB-INF/lib/目录下,支持Struts2的jar包为:commons-logging.jar、freemarker.jar、ognl.jar、struts2-core.jar、xwork.jar。
第二步,修改web.xml文件,配置Struts2的过滤器,也就是核心控制器,文件内容修改为(加粗部分为新加内容):
<?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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第三步,添加配置文件struts.xml,添加到WEB-INF/classes目录下(事实上,添加到这个目录下,在myeclipse中编辑时看不到,而且还出现过莫名其妙丢失的情况。可以把这个文件放到src下,会随着类的编译自动更新到classes目录下)。内容为:
<!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" extends="struts-default">
</package>
</struts>
将来要将package name修改为实际的包名package name,<package>和</package > 之间是要配置的Action,Result和程序拦截器Interceptor。
4、下面来做Struts2下的Helloworld。功能为:登陆页面,用户填入用户名和密码,如果用户名和密码都是'test',则转入welcome页面,输入错误转入error页面,不填继续留在登陆页面。
第一步,建立登陆页面login.jsp,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</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:form action="Login.action" method="post">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密 码"></s:password>
<s:submit value="登录"></s:submit>
</s:form>
</body>
</html>
该页面没有动态内容,既不是servlet,也不是jsp,特殊的是Login.action,表单提交给Login.action时,Struts2的FilterDispatcher会起作用,将用户请求转发到对应的Action。另外,此处使用了struts2的标签库。
第二步,实现控制器类。struts2下控制器类不需要继承父类,也无须实现任何接口,只要是一个普通的POJO就可。要实现一个execute方法,返回对用户请求的处理结果,通过这个结果,配合struts.xml配置文件,返回给用户合适的页面。实现如下:
package liu;
public class LoginAction {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception {
if ((getUsername().equals("test"))&&(getPassword().equals("test"))) {
return "success";
}
else return "error";
}
}
表面看来,该Action只是实现了两个属性的getter和setter方法,看不出请求参数在什么时候赋值给Action的属性。事实上,因为struts2有拦截器机制,它们负责解析用户的请求参数,并将请求参数赋值给Action对应的属性。
第三步,配置Action。上面实现的Action还是一个普通的POJO,没有配置在web应用中,不能处理用户请求。要使它能处理用户请求,需要将该Action配置在struts.xml中,该文件主要放置struts2的Action定义。在配置时,除了指定该Action的实现类,还需要定义Action处理结果,也就是execute方法返回值,和资源之间的映射关系,即根据返回值确定呈现给用户的响应。内容如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="liu" extends="struts-default">
<action name="Login" class="liu.LoginAction">
<!-- 定义处理结果和资源之间的关系 -->
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
到这里基本就差不多了,启动Tomcat就可以运行了,但是还少两个页面。
第四步,建立error和welcome页面,太简单了,不多说了。
- Struts2的Helloworld(版本:2.0.14)
- (2)Struts2的HelloWorld
- Struts2的HelloWorld程序
- struts2的helloworld
- struts2下的helloworld
- Struts2的HelloWorld程序
- Struts2的HelloWorld程序
- struts2的helloworld版
- struts2的HelloWorld
- 搭建struts2的helloworld
- annotation版本的helloworld
- ios版本的helloworld
- ios版本的helloworld
- Nginx版本的“helloworld”
- Struts2.1笔记(二):Struts2下的HelloWorld
- struts2 一个简单的 struts2程序(HelloWorld)
- struts2 helloworld 的404错误
- Struts2的配置与helloworld
- signal/setjmp函数使用举例
- 基于Jquery的输入前提示
- 运用HINT提高SQL语句的执行效率
- unique constraint异常捕获
- 区别 windows.h windowsx.h
- Struts2的Helloworld(版本:2.0.14)
- (转)window.opener.location.reload() and href的区别
- 泛型堆栈的c实现
- 收编娘内幕整合版(高质量PDF版)
- 缺失的6月、7月
- [lucene第三季]Lucene那点事儿-总结篇
- GDI+ Graphics类
- Spin(旋转按钮)控件
- 换水龙头