一个简单的Struts2.0小例子
2.导入struts2.0的核心包,注意全部导入全部的lib会引起错误; commons-fileupload-1.2.1.jar commons-io-1.3.2.jar freemarker-2.3.12.jar ognl-2.6.11.jar struts2-core-2.1.2.jar xwork-2.1.1.jar
3.工程配置 3.1 struts.xml 把配置文件放在src的根目录下,在struts2.0中默认的配置文件不再是struts-default.xml,而是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> <include file="struts-default.xml"></include> <package name="com" extends="struts-default"> <action name="Hello" class="com.Hello"> <result>/hello.jsp</result> </action> </package> </struts>
--其中<package name="com"中的name可以调整; <action name="Hello" class="com.Hello"> //代表Action的名称和实际存放位置 <result>/hello.jsp</result> //表示导向页面
3.2 web.xml中必须说明filter和filter-mapping的内容 <?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>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.3 Action在Struts2.0种已经把ActionForm和Action合二为一; package com;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial") public class Hello extends ActionSupport { private String message;
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; } public String execute(){ message = message + ",Hello"; return SUCCESS; }
}
3.4 index.jsp 起始页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.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> This is my first project. <br> <s:form action="Hello"> Message:<s:textfield name="message"></s:textfield> <s:submit></s:submit> </s:form> </body> </html>
3.5 hello.jsp 响应页面 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h3><s:property value="message"/> </h3><br> <a href="index.jsp">Back</a> </body> </html>