前置条件添加struts2的jar包

配置web.xml前端控制器

  <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>

配置struts.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="helloworld" extends="struts-default">
        <action name="welcome" class="com.tarena.action.WelcomeAction">
            <result name="success">/jsp/welcome.jsp</result>
            <result name="fail">/jsp/nameform.jsp</result>
        </action>
    </package>
</struts>

根据配置文件添加jsp页面

nameform.jsp静态页面
<%@page pageEncoding="utf-8"%>
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <form action="HelloWorld/welcome.action" method="get">
            <table>
                <tr>
                    <td>
                        姓名:
                    </td>
                    <td>
                        <input name="name" type="text" />
                    </td>
                </tr>

                <tr>
                    <td>
                        密码:
                    </td>
                    <td>
                        <input name="password" type="text" />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="Submit" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

 

welcome.jsp静态页面
<%@page pageEncoding="utf-8"%>
<html>
    <head>
        <title>Welcome</title>
    </head>

    <body>
        <h1>
            Welcome, ${name}
        </h1>
    </body>
</html>

添加java类

package com.tarena.action;


public class WelcomeAction{
    
    private String name;
    
    private String password;
    
    public String execute()
    {
        if ("name".equalsIgnoreCase(name))
        {
            return "fail";
        }
        return "success";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

 

posted on 2014-05-29 00:00  苏荷酒吧  阅读(141)  评论(0)    收藏  举报