code_focus

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1:创建动态Web工程

2:在web.xml里加入

<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>

3:右键单击工程,选择MyEclipse-Project Facets-Install Apache Struts(2.x)Facet

4:新建一个Java类,在此命名为HelloWorld.java,并继承ActionSupport类

  在里面重写execute()方法

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport{
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String execute() {
name = "Hello, " + name + "!"; 
return SUCCESS;
}

}

5:配置struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <include file="struts-default.xml"/>
    <package name="tutorial" extends="struts-default">
        <action name="HelloWorld" class="tutorial.HelloWorld">
            <result>HelloWorld.jsp</result>
        </action>
    </package>

</struts>    

package里的name属性代表刚才所创建Java类的包名

6:修改index.jsp代码,在<body></body>之间插入一下代码

<h3>Say "Hello" to: </h3>
        <s:form action="HelloWorld">
            Name: <s:textfield name="name" />
            <s:submit />
        </s:form>

7:新建HelloWorld.jsp,修改内容如下

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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%>">
    
    <title>My JSP 'HelloWorld.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>
    <h3><s:property value="name" /></h3>
  </body>
</html>

8:部署运行

posted on 2014-04-03 15:56  code_focus  阅读(157)  评论(1编辑  收藏  举报