struts环境搭建

1.新建web工程,我这里取名为s281.

2.复制struts最少依赖的jar文件。commons-fileupload-1.2.1, commons-logging-1.0.4 ,freemarker-2.3.15,ognl-2.7.3,struts2-core-2.1.8,xwork-core-2.1.6 把这些文件复制到WEB-INF/lib目录下面去。

3.配置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">
 
  <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>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

4.配置struts.xml

内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <package name="feng" namespace="/test" extends="struts-default">
        <action name="helloworld" class="cn.feng.action.HelloWorldAction" method="execute" >
   <result name="success">/WEB-INF/page/hello.jsp</result>
        </action>
    </package>
</struts>

 

5.在src目录下建立一个包,叫cn.feng.action.在此包下新建一个文件"HelloWorldAction.java"

内容如下:

package cn.feng.action;

public class HelloWorldAction {
 private String msg;
 
 public String getMessage() {
  return msg;
 }

 public String execute(){
  msg = "我的第一个struts2应用";
  return "success";
 }
}

6.在WEB-INF/page目录下新建hello.jsp文件,内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'hello.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>
    ${message}
  </body>
</html>

部署,然后在浏览器访问localhost:8080/s281/test/helloworld.action即可打印出“这是我的第一个struts2应用”

posted @ 2012-08-01 21:55  呼啸而过  阅读(201)  评论(0编辑  收藏  举报