Struts2 环境搭建

来源:互联网 发布:g32车螺纹编程实例 编辑:程序博客网 时间:2024/06/06 21:09

    Struts2 环境搭建   

  今天我们来讲一下如何搭建struts2 和做一个入门demo:

  step1:去官网下载struts2  点击下载  下载之后解压可以看到如下目录结构:
其中apps下面是一些war文件 ,是一写简单的空工程,doc目录是开发需要的一些歌api,lib下是一些开发中需要的库文件,需要的话导入。src下面是struts2的源码。由于之前在学校学习java的时候一直用的是MyEclipse,殊不知他是收费的,到了公司项目组之后,公司用的是eclipse,那么我们在Eclipse中集成struts不像在MyEclipse中那么简单,直接由写好的插件一点击就可以吧sruts的功能给集成进去。
step2:
注意:
我首先说一下我今天之前出现的错误,我是之前看到一篇文章中写的是为了养成一个良好的开发习惯,提高开发的效率等,先在window->properties->javabuild->librairy中先new一个自己的库(struts中的那些个必须的jar包),然后之后开发的时候直接使用,项目 右键builpath就导入自己的那个库就ok ,但是我也这样搞了,在后面web.xml中加入struts的过滤器,服务器一直起不起来,报错说找不到这个dispatherFilter那个类。最终我还是把那个删了重新搞得。
step3:

新建自己的web项目
注意:
如果建立web项目的时候选择的是Dynamic web version 为3.0的话,那么生成的项目中和2.0的有一些区别,别如说没有了WEB-INF下的web.xml,tomcat7支持了无配置的格式,在代码中直接写注解代替xml的配置,由于我不是很熟悉注解这个东西,所以这里我在手动的给加一个web.xml文件。
step4:

在struts包的解压目录lib中找到这写jar包直接复制到工程的lib文件中,然后在web.xml 中添加struts2的过滤器代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <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>
</web-app>
  然后保存。
step5:

 在src目录下添加struts.xml
 这个位置很重要,要加对
 里边的配置代码如下:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>


   
    <package name="a" extends="struts-default">


       <action name="helloworld" class="com.soft.grand.HelloWorldAction">


           <result>/result.jsp</result>


       </action>


       
    </package>


</struts>
其中action name和jsp中的action 要一致,class为自己的包名字+类名
package name随便写,extends "struts-default"为固定的
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
     <form action="helloworld" method=post>
        <input type="submit"/>
     </form>
</body>
</html>

result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
           <span > result.jsp</span>
</body>
</html>

Action :

package com.soft.grand;


import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class HelloWorldAction extends ActionSupport {
      public String execute(){
     System.out.println("hello world!!");
     return "success";
      }
}

web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <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>
</web-app>
  
 这样一个简单的struts环境就这样搭建起来了
0 0
原创粉丝点击