Ronnie ......

Struts2入门之HelloWorld

 

1. 新建Web ProjectFile——>New——>Project);

选择MyEclipse——>Web Project,点击“Next

输入工程名,点击“Finish”;

展开工程,

右键点击libWebRoot——>WEB-INF——>lib),

点击Import导入struts2所需的5jar

打开web.xml,添加

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

       <display-name>Struts 2.0 Hello World</display-name>

       <!-- 配置过滤器,控制所有http请求,类似于struts1ActionServlet -->

       <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.html</welcome-file>

       </welcome-file-list>

</web-app>

右键单击src

选择New——>File,新建struts.xml文件,添加如下内容:

<!DOCTYPE struts PUBLIC

         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

         "http://struts.apache.org/dtds/struts-2.0.dtd">

 <struts>

        <!-- 加载默认的 struts2 配置文件,struts-default.xml包含在struts2-core-2.0.11.1.jar包中

               此处指定可以加载多个配置文件,action的配置可以写在其他配置文件中 -->

     <include file="struts-default.xml"/>

        

        <!-- 继承默认的 struts2 配置文件;Struts2Action必须放在指定的包空间下定义,package标签定义包空间,可以包含多个action -->

        <package name="test.struts" extends="struts-default">

               <!-- 请求名称和class类的映射关系,页面请求此action时,请求urlname属性值+".action" -->

         <action name="HelloWorld" class="test.struts.HelloWorld">

             <!-- 处理结果和页面的映射关系,可以添加多个<result></result>标签;

                             不指定name属性时,默认为success -->

                      <result name="success">HelloWorld.jsp</result>

         </action>

     </package>

 </struts>

新建test.struts,新建类HelloWorld,内容如下:

package test.struts;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

       private String name;

       public HelloWorld() {

              super();

       }

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       //execute方法时请求action默认调用的方法

       public String execute() {

              name = "Hello, " + name + "!";

              return SUCCESS; //返回字符串对应配置文件中的result标签的name属性

       }

}

新建sayHello.jsp,内容如下:

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

       <head>

              <title>Say Hello</title>

       </head>

       <body>

              <h3>

                     Say "Hello" to:

              </h3>

              <!-- 如果此处不使用struts标签,使用一般的html标签<form>,应注意action="HelloWorld.action"

              .actionstruts2定义的后缀,类似于struts1中的.do; -->

              <s:form action="HelloWorld">

             Name: <s:textfield name="name" />

                     <s:submit />

              </s:form>

       </body>

</html>

新建HelloWorld.jsp,内容如下:

<%@ page contentType="text/html; charset=UTF-8" %>

 <%@ taglib prefix="s" uri="/struts-tags" %>

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 <html>

     <head>

         <title>Hello</title>

     </head>

     <body>

         <h3><s:property value="name" /></h3>

     </body>

 </html>

部署应用:

方法一:

MyEclipse提供了方便的功能部署应用,此部署功能其实就是把应用拷贝到TomcatWebApps目录下;

方法二:

tomcat_home"conf"Catalina"localhost目录下新建XML文件指定应用位置,而不是拷贝应用到WebApps目录下,如新建testStruts2.xml文件,内容如下:

<Context path="/testStruts" docBase="E:"eclipse_workspace"testStruts2"WebRoot"

        debug="0" privileged="true">

</Context>

其中path属性指定应用名,可任意,docBase属性指定应用的路径;

注意:Tomcat5.5以上版本中,应用名和XML文件名必须一致,在Tomcat5.0版本中文件名和应用名可以不同;

我个人习惯用方法二部署,感觉比较方便,修改应用后不用重新部署,而采用方法一的话每次修改了java类或者xml文件后要点击部署。

部署在Tomcat5时注意:

xalan系列jar包(serializer.jarxalan.jarxercesImpl.jarxml-apis.jar)替换原来的tomcat_home"common"endorsed目录下的xercesImpl.jarxml-apis.jar

不然会出现:严重: Error filterStart,无法部署的情况;

部署时出现警告的解决方法

警告: Settings: Could not parse struts.locale setting, substituting default VM locale

出现此警告的解决方法:

创建struts.properties这个文件,文件内容如下:

struts.locale=en_utf-8或者struts.locale=en_GB

struts.properties文件路径添加到classpath中,即放在web应用的WEB-INF目录下的classes目录下,而具体此文件的作用还不清楚。

posted on 2008-10-20 21:08 ronnie 阅读(141) 评论(0)  编辑  收藏 所属分类: Struts


只有注册用户登录后才能发表评论。


网站导航: