eclipse 新建 struts项目

eclipse中新建struts工程 


1.新建一个动态网页工程webStruts。
 


注意设置classes路径为:webStruts/WebContent/WEB-INF/classes
 


2.引入jar包,如下图:(其中每个jar可以说都是必须的)
 


3.编写web.xml。
 
<?xmlversion="1.0"encoding="UTF-8"?>
 
<web-appid="WebApp_9"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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 
 
   <display-name>webStruts</display-name>
 
   <filter>
 
        <filter-name>struts2</filter-name>
 
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 
   </filter>
 
<filter-mapping>
 
        <filter-name>struts2</filter-name>
 
        <url-pattern>/*</url-pattern>
 
   </filter-mapping>
 
<welcome-file-list>
 
        <welcome-file>querybooks.jsp</welcome-file>
 
   </welcome-file-list>
 
</web-app>
 
 
 
4.编写querybooks.jsp。
 
<%@ page language="java"pageEncoding="UTF-8"%>
 
<%@ taglib prefix="s"uri="/struts-tags"%>
 
<html>
 
    <head>
 
        <title>查询图书信息</title>
 
    </head>
 
<body> 
 
         <s:formaction="query">
 
            <s:textfieldlabel="书名"name="name"/>       
 
            <s:submitvalue="查询"/>
 
        </s:form>
 
    </body>
 
</html>
 
 
 
 
 
5.下一步我们编写action。新建一个包com.yuqiaotech.actions,在该包下新建一个QueryAction。新建一个包com.yuqiaotech.DAO,在该包下新建一个实体类BookDAO。
 
QueryAction.java
 
package com.yuqiaotech.actions;
 
import com.yuqiaotech.DAO.*;
 
import java.util.*;
 
import org.apache.struts2.*;
 
import javax.servlet.http.HttpServletRequest;
 
import com.opensymphony.xwork2.ActionContext;
 
publicclassQueryAction
 
{
 
    private Stringname;
 
private Map<String,Integer>result;
 
    public Map<String,Integer> getResult()
 
    {
 
        returnresult;
 
    }
 
    publicvoidsetResult(Map<String, Integer> result)
 
    {
 
        this.result = result;
 
    }
 
    public String getName()
 
    {
 
        returnname;
 
    }
 
    publicvoid setName(String name)
 
    {
 
        this.name = name;
 
    }
 
    public String execute()throws Exception
 
    {
 
        try
 
        {
 
    HttpServletRequestrequest = (HttpServletRequest) ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
 
    System.out.println(request.getCharacterEncoding());
 
            BookDAO book = new BookDAO();
 
            Map<String,Integer> books =  book.getBooks(name);     
 
            

            request.setAttribute("result",books);
 
            setResult(books);
 
             return"result";
 
         //returnSUCCESS;
 
        }
 
        catch (Exception e)
 
        {
 
            return"error";
 
        }
 
    }
 
}
 
BookDAO.java
 
package com.yuqiaotech.DAO;
 
import java.util.*;
 
publicclassBookDAO
 
{   
 
    privatestatic Map<String,Integer>books=newHashMap<String, Integer>();
 
   static
 
   {
 
    books.put("J2EE整合详解与典型案例", 79);
 
    books.put("VISUAL C# 2008开发技术实例祥解", 89);
 
    books.put("STRUTS 2技术详解", 69);
 
    books.put("ASP经典模块开发大全", 69);
 
    books.put("ASP.NET 3.5网络数据库开发实例自学手册", 79);
 
    books.put("XML开发典型应用:数据标记、处理、共享与分析", 65);
 
   }
 
   //
 
   publicMap<String, Integer> getBooks(String name)
 
   {
 
    Map<String, Integer> books =newLinkedHashMap<String, Integer>();
 
   
 
    for(Map.Entry<String, Integer> entry:BookDAO.books.entrySet())
 
    {
 
        if(entry.getKey().toLowerCase().contains(name.toLowerCase()))
 
            books.put(entry.getKey(),entry.getValue());
 
    }                     

    return books;
 
   }
 
   
 
}
 
 
 
 6.编写struts.xml,直接放在src目录下即可
 
<?xmlversion="1.0"encoding="UTF-8"?>
 
<!DOCTYPEstrutsPUBLIC
 
    "-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"
 
    "http://struts.apache.org/dtds/struts-2.3.dtd">
 
<struts>
 
 <packagename="struts"  extends="struts-default">  
 
   <actionname="query" class="com.yuqiaotech.actions.QueryAction">
 
  <resultname="result">/result.jsp</result>
 
   </action>
 
   </package>
 
</struts>
 
 
 
7.编写result.jsp
 
<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>
 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%>
 
<%@ taglib prefix="s"uri="/struts-tags"%>
 
 <html>
 
   <head>
 
     <title>查询结果</title>
 
   </head>
 
   <body>
 
   使用Java代码获得查询结果<br/>
 
    <tableborder="1">
 
        <tr>
 
            <td>书名</td>
 
            <td>价格</td>
 
        </tr> 
 
            Map<String, Integer> result =(Map<String,Integer>) request.getAttribute("result");
 
            for(Map.Entry<String, Integer> entry: result.entrySet())
 
            {
 
        %>
 
        <tr><td>
 
        <%= entry.getKey()%>
 
        </td><td>
 
        <%= entry.getValue()%>
 
        </td>
 
        </tr>
 
        <%}%>    
 
 
 
    </table> 
 
    <p/>  
 
          使用JSP标签获得查询结果<br/>
 
          <tableborder="1">
 
        <tr>
 
            <td>书名</td>
 
            <td>价格</td>
 
        </tr>
 
       <c:forEachvar="entry"items="${result}">
 
        <tr><td>
 
         ${entry.key}
 
        </td><td>
 
         ${entry.value}
 
        </tr>
 
        </c:forEach> 
 
    </table>
 
    <p/>
 
          使用Struts2标签获得查询结果<br/>
 
          <tableborder="1">
 
        <tr>
 
            <td>书名</td>
 
            <td>价格</td>
 
        </tr>
 
 
 
        <s:iteratorvalue="result">
 
        <tr>
 
        <td><s:propertyvalue="key"/></td>
 
        <td><s:propertyvalue="value"/></td>
 
        </tr>
 
        </s:iterator> 
 
    </table>
 
   </body>
 
  </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值