struts入门例子

struts例子

1.找到struts相关的jar文件,拷贝到WEB-INF\LIB目录中,应该是八个jar文件:
antlr.jar,
commons-beanutils.jar,
commons-digester.jar,
commons-fileupload.jar,
commons-logging.jar,
commons-validator.jar,
jakarta-oro.jar,
struts.jar
2.导入到项目tomcat2中,在tomcat2项目中右键选Properties->Java Build path->Libraries->Add External JARS...,全选WEB-INF\LIB中的八个jar文件.
3.创建HelloAction.java文件.在WEB-INF/src中选择test包,然后右键创建一个新类。
name:HelloAction
Superclass:Browse->输入action->选择Action-org.apache.struts.action
4.打开HelloAction.java后,右键选source->Override/Implement Methods->execute
这样我们就重载/实现了父类的execute方法.因为父类的execute有二个同名方法,选择参数为Http开头的.
将:
public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
  // TODO Auto-generated method stub
  return super.execute(arg0, arg1, arg2, arg3);
}
修改:
public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
  // TODO Auto-generated method stub
    return arg0.findForward("success");
}
5.在WEB-INF目录上右键创建一个web.xml,这里利用Myeclipse插件带的xml项目来创建.
MyEclipse->XML->XML(Advanced Templates)
fileName:web.xml
Template to use:XML template for a web.xml file with Struts
保存时会提示已经有一个web.xml了(上个实例创建的),这里覆盖它.
打开web.xml这时已经为建立了模板,经过修改:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>

<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>2</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>2</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>

6.在WEB-INF目录上右键创建一个struts-config.xml,这里利用Myeclipse插件带的xml项目来创建.
MyEclipse->XML->XML(Advanced Templates)
fileName:struts-config.xml
Template to use:XML template for a struts-config.xml file
打开struts-config.xml经过修改:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<action-mappings>
   <action path="/hiAction" type="test.HelloAction">
   <forward name="success" path="/hello.jsp"></forward>
   </action>
 
</action-mappings>
</struts-config>

7.在tomcat2上右键新建一个jsp文件.filename:hello.jsp
源码:
<%@ page contentType="text/html; charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Struts Action</title>
</head>
<body>
利用struts Action调用jsp文件的例子
</body>
</html>

8.将tomcat服务停止一下,然后打开.
输入 http://localhost/hiAction.do
这时可以看到打开了hello.jsp文件.
注意:不管是java代码还是xml都是区分大小写的。

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