趁着周末来重温下struts,竟然花了我一个多小时来配置和调试才弄好一个最简单的例子!
三年前的"老爷机"跑netbean就是慢。。。惨啊。。。
在betbean里新建一个WebApplication1的web项目,完成后,就在项目的WEB-INF目录下新建一个struts-config.xml文档,
具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : struts-config.xml
Created on : 2009骞?鏈?鏃? 涓嬪崍8:15
Author : sharekw
Description:
Purpose of the document follows.
-->
<!DOCTYPE action PUBLIC '-//Apache Software Foundation//DTD Struts Configuration 1.2//EN' 'null'>
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.qingwa.struts.actionForm.LoginActionForm"/>
</form-beans>
<action-mappings>
<action name="loginForm" path="/login" type="com.qingwa.struts.action.LoginAction">
<forward name="success" path="/page/loginSucess.jsp"/>
</action>
</action-mappings>
</struts-config>
现在分析下这个文档,<form-bean name="loginForm" type="com.qingwa.struts.actionForm.LoginActionForm"/>这里的name是可以随便取,专门是给
<action-mappings>
<action name="loginForm" path="/login" type="com.qingwa.struts.action.LoginAction">
<forward name="success" path="/page/loginSucess.jsp"/>
</action>中的name引用的,注意,这两个一定要一致;其他的一看就知道怎么用了。
在<action>标签里,path属性一定要跟jsp中<form action="/login.do">中的action要一致,否则会找不到路径,这里的path="/login",而不是/login.do,因为下面的web.xml中,已经配好了,全部以.do结尾的路径都会转到struts中的action里来。
我的jsp和另外两个类代码如下:
<%--
Document : index
Author : sharekw
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<form action="/login.do" method="post">
<input type="text" name="id"/>
<br>
<input type="text" name="name"/>
<br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
LoginAction:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.qingwa.struts.action;
import com.qingwa.struts.actionForm.LoginActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
/**
*
* @author sharekw
*/
public class LoginAction extends org.apache.struts.action.Action
{
/* forward name="success" path="" */
private final static String SUCCESS = "success";
/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws java.lang.Exception
* @return
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
LoginActionForm loginForm = (LoginActionForm) form;
return mapping.findForward(SUCCESS);
}
}
注意:上面findForward(SUCCESS)的路径要跟struts-config.xml中action标签中的<forward>下的name一致。否则出现找不到错误。
LoginActionForm:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.qingwa.struts.actionForm;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
*
* @author sharekw
*/
public class LoginActionForm extends ActionForm
{
private String id;
private String name;
private String password;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
return errors;
}
}
现在,已经配置好了struts-config.xml和写好了jsp和两个类;最后只差配web.xml了,
具体的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
web.xml配置比较容易理解,一看就知道怎么用了。
把它deploy到jboss或其它的中间件中,就可以运行了。