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">
<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">
<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>
 
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">
<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>
 
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>
 
注释:这里使用了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>
 
注释:这里的:<s:property value="message" />!,则是简单的显示出message传过来的值。
 
9.基本上此基于struts2的HelloWorld程序建立完毕,运行http://localhost:8080/HelloStruts2/
便可出现运行界面,界面显示就不列出了,整个程序运行是正常的。

 如有错误,请给出指正……

原创粉丝点击