Struts2笔记(1:项目搭建及最简单的例子)

首先struts2所用到的jar包:

新建一个web项目,对应的文件结构如图:

问题:

eclipse下classes文件夹无法发布到tomcat的问题(即Eclipse项目下WEB-INF中无classes文件夹或classes为空)

解决方案:

问题出在eclipse的.classpath文件,里面有这样一条:
可以正常运行的项目是这样写的:
<classpathentry kind="output" path="target/classes"/>
不能正常运行的项目的.classpath是这样写的:
<classpathentry kind="output" path="WebContent/WEB-INF/classes"/>

(注:Eclipse生成的web项目是WebContent,MyEclipse生成的web项目是WebRoot

 

HelloWorldAction类:

 

package cn.itcast.action;

public class HelloWorldAction{

    private String message;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String execute()
    {
        message = "第一个应用qwedqwedqwdqwdqwd";
        return "success";
    }
}

 

struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--指定struts2配置文件的DTD信息-->
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
<!--struts2配置文件根元素-->          
<struts>
    <package name="itcast" namespace="/test" extends="struts-default" >
        <action name="hello" class="cn.itcast.action.HelloWorldAction" method="execute">
            <result name="success">/WEB-INF/page/hello.jsp</result>
        </action>
    </package>
</struts>

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
    <web-app 
        id="GwFxFos" 
        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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    
    <!-- struts filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <welcome-file-list>
        <welcome-file>page/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

page文件夹下hello.jsp文件:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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=utf-8">
<title>登陆成功</title>
</head>
<body>
   ${message}
</body>
</html>    

 

部署后tomcat运行结果如下:(后缀.action)

注:如配置文件或其他文件修改要清除缓存,重新构建项目生成新的文件(最干净的方法:清除tomcat的work路径下文件,清除WEB-INF下classes文件夹下的文件,重新构建运行)

 

附加:

有时开发时Eclipse中编辑struts.xml文件无智能提示,因为dtd文件未加载进来,解决方法:1:联网  2:找到:http://struts.apache.org/dtds/struts-2.0.dtd文件,添加进来,方法:Eclipse->window->Preference->...->XML Catalog   add,选择文件的路径,key type选择URI,key粘贴:http://struts.apache.org/dtds/struts-2.0.dtd  OK!!!

 

 

posted on 2014-07-09 16:01  11133432211  阅读(159)  评论(0)    收藏  举报