rrl
来源:互联网 发布:linux卸载jdk1.8 编辑:程序博客网 时间:2024/05/21 07:06
弄了几次的struts2开发环境搭建,几次都没成功,还以为自己的机器问题啊!原来不是这样的哦。
这几天没事也继续搭建一下,也去找了些简单的实例来试着运行,终于将这个struts2环境搭建完毕。
开发环境:Eclipse3.2+Lomboz+JDK1.5+JBOSS4(Tomcat5)
这里将这个搭建过程记录下来,最基础的入门HelloWorld应用程序:
1.建立一个工程,一般都是直接建立一个动态网站的工程,并不直接建立struts project,理由就是到时候
在工程里面出现两个web.xml文件,容易混淆。
2.将能支持struts2的必要包,导入,包就不一一列出。
3.其次是配置能开发struts2的必要环境,首先给web.xml定义如下:
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>HelloWorld</display-name>
<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>
</web-app>
<display-name>HelloWorld</display-name>
<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>
</web-app>
4.然后配置出struts.xml文件,注意这里的struts.xml文件需放在src目录下面,便于程序启动时
可以直接加载,访问的时候实现正常的映射功能。文件配置如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts><!-- Configuration for the default package. -->
<package name="tutorial" namespace="/" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result name="success">/helloworld.jsp</result>
</action>
</package>
</struts>
<package name="tutorial" namespace="/" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result name="success">/helloworld.jsp</result>
</action>
</package>
</struts>
5.新建一个包,tutorial(名字可自己定义),在其下面建立HelloWorld.java文件,用于处理访问
时候的程序指令,文件如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts><!-- Configuration for the default package. -->
<package name="tutorial" namespace="/" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result name="success">/helloworld.jsp</result>
</action>
</package>
</struts>
<package name="tutorial" namespace="/" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result name="success">/helloworld.jsp</result>
</action>
</package>
</struts>
6.新建一jsp页面,index.jsp。当访问时候直接从此页面进入,文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>首页</title>
</head>
<body>
<h2><s:form action="HelloWorld.action">
<s:textfield name="message" label="你的姓名"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form></h2>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>首页</title>
</head>
<body>
<h2><s:form action="HelloWorld.action">
<s:textfield name="message" label="你的姓名"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form></h2>
</body>
</html>
注释:这里使用了struts的tag标签<%@ taglib prefix="s" uri="/struts-tags"%>,需要使用
的时候,必须进行注册。当然有时候也可以不用此标签。
7.新建一jsp页面HelloWorld.jsp,顾名思义,此页面是进行访问后的结果页面,文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hello World</title>
</head>
<body>
你好:<s:property value="message" />!
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hello World</title>
</head>
<body>
你好:<s:property value="message" />!
</body>
</html>
注释:这里的:<s:property value="message" />!,则是简单的显示出message传过来的值。
9.基本上此基于struts2的HelloWorld程序建立完毕,运行http://localhost:8080/HelloStruts2/
便可出现运行界面,界面显示就不列出了,整个程序运行是正常的。
如有错误,请给出指正……
- rrl
- RRL Protocol修改注册表,在浏览器输入程序参数,运行程序
- 查看linux的版本主要有三种方法
- 分布式缓存系统Memcached简介与实践
- tcp/ip详解学习---tcp
- ORACLE UPDATE 语句语法
- Web服务器证书应用配置手册(四)
- rrl
- 测试Web应用程序是否存在跨站点脚本漏洞
- Web服务器证书应用配置手册(五)
- linux 中文显示乱码解决办法
- Flex 相关电子书籍下载链接收录
- 用NVPerfHUD来分析3D程序
- asp.net 上传图像实例
- 我的Maven2之旅:五.pom.xml基本元素介绍
- Displaytag (partialList ="true") 分页例子子