Struts2的Hello World!

1.用MyEclipse New一个Web Project:struts2

2.在http://people.apache.org/builds/struts/上下载所需的包
  然后在WebRoot/WEB-INF导入Struts2的包:
  commons-logging-1.0.4.jar
  freemarker-2.3.4.jar
  junit-3.8.1.jar
  ognl-2.6.7.jar
  oscore-2.2.4.jar
  struts2-api-2.0.0-SNAPSHOT.jar
  struts2-core-2.0.0-SNAPSHOT.jar
  struts2-extras-2.0.0-SNAPSHOT.jar 
  xwork-2.0.0-SNAPSHOT.jar

3.在src/com/test下建立TestAction.java类:
package com.test;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class TestAction extends ActionSupport {
	//跟页面上的属性对应(FormBean)
	private String name;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	/***
	 * Struts2中已定义好静态变量
	 * SUCCESS = "success"
	 * NONE = "none"
         * ERROR = "error"
	 * INPUT = "input"
	 * LOGIN = "login"
	 * @return
	 */
	public String skipPage(){
		return SUCCESS;
	}
}


4.在src下建立struts.xml跟struts.properties文件:
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 先加载Struts2-core-2.0.0-SNAPSHOT.jar中的struts-default.xml文件 -->
	<include file="struts-default.xml" />
	<!-- 继承Struts-core.jar中的配置文件 -->
	<package name="default" extends="struts-default">
		<!-- 加载具体的Action类的业务方法skipPage,test.action为链接 -->
		<action name="test" class="com.test.TestAction" method="skipPage">
			<!-- 由type来指定FreeMaker模板、Velocity模板、JasperReports和使用XSL转换等。 -->
			<result name="success" type="">/success.jsp</result>
		</action>
	</package>
</struts>

struts.properties:(暂时不用,可以先清空即可)

5.在WebRoot/WEB-INF下建立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">
	<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>


6.在WebRoot/WEB-INF下建立test.jsp跟success.jsp:
test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
  <body>
    	<s:form action="test">
			<s:textfield label="请输入名称" name="name"/>
			<s:submit />
		</s:form>
  </body>
</html>

success.jsp:
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
	<body>
		<s:property value="name"/>跳转到正确页面.
	</body>
</html>

你可能感兴趣的:(apache,jsp,Web,xml,struts)