您的位置:首页 > 编程语言 > Java开发

第一个struts2程序

2013-11-28 21:10 274 查看
搭建struts2

引入struts的jar包

这可以自己下载。然后在lib下找

web.xml放在WEB-INF下

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

<web-app id="WebApp_9" 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">
    <display-name>Struts</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.jsp</welcome-file>

    </welcome-file-list>

</web-app>

struts放在src下

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

<package name="default" namespace="/" extends="struts-default">

        <action name="student" class="com.ljq.action.studentAction">

            <!--<result type="studentAction">

                <param name="error">error.jsp</param>

                <param name="welcome">welcome.jsp</param>

                <param name="index">index.jsp</param>

            </result>

        -->

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

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

        </action>


编写java代码

studentAction

package com.ljq.action;

public class studentAction {

    

    private int stuId;

    private String name;

    private String password;

    private String sex;

    private int age;

    public String execute(){

        

        /*System.out.println("stuId"+stuId);

        System.out.println("name"+name);

        System.out.println("password"+password);

        System.out.println("sex"+sex);

        System.out.println("age"+age);*/

        if("ljq".equals(getName())&&"123".equals(getPassword())){

            return "success";

        }

        else{

            return "error";

        }


        


    }

    public int getStuId() {

        return stuId;

    }

    public void setStuId(int stuId) {

        this.stuId = stuId;

    }

    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;

    }

    public String getSex() {

        return sex;

    }

    public void setSex(String sex) {

        this.sex = sex;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

}

login.jsp

 <body >

  <h1>欢迎使用信息学院信息管理系统学院</h1>

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

    用户名<input type="text" name="name"/><br/>

    密码<input type="password" name="password"/><br/>

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

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

    </form>

  </body>

error.jsp

<body>

    <h1>您输入的信息错误,请重新登陆</h1>

    <form action="student" method="post"></form>

    用户名<input type="text" name="name"/><br/>

    密码<input type="password" name="password"/><br/>

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

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

    </form>

  </body>

welcome.jsp

<body>

    登录成功,欢迎使用

  </body>

经过以上的工作,整个代码就可运行了。

/*有啥问题大家一起交流哦*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息