Struts2.0入门

来源:互联网 发布:大番长数据修改器 编辑:程序博客网 时间:2024/05/19 03:22

版本:eclipse 3.3myeclipse 6.0tomcat 6.0
myeclipse中新建项目StrutsDemo,在struts官网下载struts2.0 jar
并将jar包拷贝到项目lib
myeclipse版本为6.5则可以直接导入struts2.0
核心包:commons-logging-api-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.8.jar
xwork-2.0.3.jar

com.shiryu.demo包中新建HelloAction.java

package com.shiryu.demo;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
    private String msg;

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        if("kava".equalsIgnoreCase(msg)) {
            return"suc";
        } else {
            return"err";
        }
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
       System.out.print("*******************");
        this.msg = msg;
    }

}

配置web.xml文件

<?xml version="1.0"encoding="UTF-8"?>
<web-app 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">
   <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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

配置struts.xml(struts.xmlsrc文件夹中)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="zhaiyu"namespace="/" extends="struts-default">
       <action name="hello"class="com.shiryu.demo.HelloAction">
         <resultname="suc">
           /hello.jsp
         </result>
         <resultname="err">
           /errors.jsp
         </result>
       </action>
    </package>

</struts>

新建index.jsp

<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>My JSP 'index.jsp' starting page</title>
</head>

<body>
    <form action = "hello.action" method ="post">
     
输入内容:<input type="text" name="msg" />
            <input type="submit" value="
提交"/>
    </form>
</body>
</html>

新建hello.jsp

<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
   
输入内容为:<s:property value="msg"/>
</body>
</html>

新建errors.jsp

<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>My JSP 'errors.jsp' starting page</title>
</head>

<body>
   
输入内容有错误!!!!
</body>
</html>

 

原创粉丝点击