首页 | 联系我们 | 叶凡网络官方QQ群:323842844
游客,欢迎您! 请登录 免费注册 忘记密码
您所在的位置:首页 > 开发语言 > Java开发 > 正文

struts入门小实例

作者:cocomyyz 来源: 日期:2013-8-18 8:06:13 人气:0 加入收藏 评论:0 标签:java

我认为每一门课程的入门都要从例子开始,脑中有概念的时候就会很容易上手,就像我一开始看
《证券投资分析》,不知所云,于是找来同学,领我入门,结果他给我举了好多例子,好了,
我终于有点能摸得着门了。

下面就以一个例子来入struts这个门:

第一步:建个包com.brj.strutsdemo

第二步:将struts的jar包导入(用myeclipse会快些)

第三步:写User.java类

===================================================
package com.brj.strutsdemo;

/**
* the simple object
*
* @author brj 2008-09-09
*
*/
public class User {
private String userName;
private String password;

public String getUserName() {
  return userName;
}

public void setUserName(String userName) {
  this.userName = userName;
}

public String getPassword() {
  return password;
}

public void setPassword(String password) {
  this.password = password;
}
}
=================================================

第四步:写UserGenerator.java类

package com.brj.strutsdemo;

import java.util.ArrayList;
import java.util.List;

/**
* Generate some 'User' to be the test data
*
* @author brj 2008-09-09
*
*/
public class UserGenerator {

public static List<User> getUsers() {
  List<User> list = new ArrayList<User>();
  User user = null;
  for (int i = 0; i < 6; i++) {
   user = new User();
   user.setUserName("admin" + i);
   user.setPassword("brj" + i);
   list.add(user);
  }
  return list;
}
}
=====================================================

第五步:写UserListAction.java

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.brj.strutsdemo.action;

import java.util.List;

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;

import com.brj.strutsdemo.User;
import com.brj.strutsdemo.UserGenerator;

/**
* MyEclipse Struts
* Creation date: 09-08-2008
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class UserListAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  List<User> users = UserGenerator.getUsers();
  request.setAttribute("users", users);
  return mapping.findForward("list");
}
}
===========================================


第六步:在web.xml中写如下面的东西,

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>
   <init-param>
     <param-name>detail</param-name>
     <param-value>3</param-value>
   </init-param>
   <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>action</servlet-name>
   <url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

=================================================
第七步:在struts-config.xml文件中写如下面的东西,

<?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="/userList" type="com.brj.strutsdemo.action.UserListAction">
   <forward name="list" path="/index.jsp"></forward>
   </action>
</action-mappings>

<message-resources parameter="com.brj.strutsdemo.ApplicationResources" />
</struts-config>
=================================================================
第八步:写index.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>
<c:if test="${users == null}">
<a href="userList.do">Show All User</a>
</c:if>
   <table style="widht:500;">
   <tr>
      <th>List All User:</th>
   </tr>
   <tr>
      <td>UserName</td>
      <td>Password</td>
   </tr>
   <c:forEach items="${users }" var="user">
   <tr>
      <td>${user.userName }</td>
      <td>${user.password }</td>
   </tr>
   </c:forEach>
   </table>
</body>
</html>


================================================

第九步:没了,真的没了


本文网址:http://www.mingyangnet.com/html/java/196.html
读完这篇文章后,您心情如何?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
更多>>网友评论
发表评论
编辑推荐
  • 没有资料