第一个Struts2程序

Struts2.3.16, Tomcat6.0.37,Java8

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

      <display-name>blk</display-name>



    <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.html</welcome-file>

    </welcome-file-list>

    

</web-app>

 

/WEB-INF/classes/struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">



<struts>



    <constant name="struts.enable.DynamicMethodInvocation" value="false" />

    <constant name="struts.devMode" value="true" />



    <!-- Add packages here -->

    

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

        <action name="XXX" class="demo2d4.action.LoginAction">

            <result name="error">/demo2d4/error.jsp</result>

            <result name="success">/demo2d4/welcome.jsp</result>

        </action>

    </package>



</struts>

 

/demo2d4/login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<!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>Insert title here</title>

</head>

<body>

    <form action="XXX.action" method="post">

        <table align="center">

            <caption><h3>用户登录</h3></caption>

            <tr>

                <td>用户名:<input type="text" name="username"/></td>

            </tr>

            <tr>

                <td>密码:<input type="password" name="password"/></td>

            </tr>

            <tr align="center">

                <td colspan="2">

                    <input type="submit" value="登录"/>

                    <input type="reset" value="重置">

                </td>

            </tr>

        </table>

    </form>

</body>

</html>

 

/demo2d4/welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<!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>Insert title here</title>

</head>

<body>

    您已经登录

</body>

</html>

 

/demo2d4/error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<!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>Insert title here</title>

</head>

<body>

    <span style="color:red">登录失败</span>

</body>

</html>

 

demo2d4.action.LoginAction.java

package demo2d4.action;



public class LoginAction {

    private String username;

    private String password;

    public String getPassword() {

        return password;

    }

    public String getUsername() {

        return username;

    }

    public void setPassword(String password) {

        this.password = password;

    }

    public void setUsername(String username) {

        this.username = username;

    }

    public String execute() throws Exception {

        if (getUsername().equals("scott")&&getPassword().equals("tiger")) {

            return "success";

        } else {

            return "error";

        }

    }

}

 

/WEB-INF/lib; tomcat/lib

见http://www.cnblogs.com/qrlozte/p/3775774.html

你可能感兴趣的:(struts2)