第一个struts2程序

对于初学者来说 ,struts2的配置相当复杂 。我现在分享一个完整的例子给与大家参考

web.xml

<?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.xml

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

action

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

<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>
welcome.jsp

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿卢

谢谢鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值