追风dē洒意

常用链接

统计

最新评论

2008年11月6日 #

Struts的第一个入门例子

使用软件myeclipse6.5

1新建--Web Project--输入Project name:TestStruts
2项目建好后.选中项目.myeclipse的菜单栏上有MyEclipse--Project Capabilities--Add Struts Capabilities-----finish


3打开WebRootWEB-INF/struts-config.xml选择Source
输入如下代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans />
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      path="/Hello"
      scope="request"
      type="net.blogjava.youzi.HelloWordAction" >
      <forward name="H1" path="/Hello1.jsp" />
      <forward name="H2" path="/Hello2.jsp" />
    </action>

  </action-mappings>

  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
你也可以选择Design进行可视化拖拉

4打开src/net.blogjava.youzi/HelloWordAction.java
代码如下:
package net.blogjava.you;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class HelloWordAction extends Action {
 
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  String para = request.getParameter("place");
  if(para.equals("1"))
   return (mapping.findForward("H1"));
  else
   return (mapping.findForward("H2"));
 }
}

5接着打开/WebRoot/index.jsp
在body中输入
    <a href = "Hello.do?place=1">go to Hello1</a>
    <a href = "Hello.do?place=2">go to Hello2</a>

保存
6打开/WebRoot/Hello1.jsp
在body中输入
this is show page, the page name is Hello1.jsp
7打开/WebRoot/Hello2.jsp
在body中输入
this is show page, the page name is Hello2.jsp
8布署工程,启动tomcat
9浏览器输入:http://localhost:8080/TestStruts


点击go to Hello1链接后,可以看到Hello1.jsp的内容.




这时恭喜你,成功入门了.

posted @ 2008-11-06 13:50 追风dē洒意 阅读(150) | 评论 (0)编辑 收藏

仅列出标题