Struts2处理数据

本文介绍了Struts2框架的数据处理流程,通过用户登录案例详细解释了从项目搭建到实现登录验证的整个过程,包括配置文件、页面设计及后台逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一节入门讲了Struts2如何将请求映射到类中,这一节讲Struts2如何处理数据和提交数据。通过用户登录案例来观察Struts2数据处理的过程。


步骤:

1 . 建项目,导jar包
2 .配置web.xml——配置struts2的核心过滤器

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>03Struts2_login</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>
    <!--推荐使用*.action  效率更高   不然.jsp也要过一遍  -->
    <url-pattern>*.siggy</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3 .在src下编写struts.xml(头文件,内容先空着)
4 .编写login.jsp

 <body>
    <form  action="login.action" method="post" >
    用户名:<input type="text"  name="name"/><br>
    密码:<input type="password"  name="pwd"/><br>
    <input type="submit"  value="登录"/>
    </form>
  </body>

5.编写LoginAction类

package cn.sxt.siggy;

public class LoginAction {
    private String name;
    private String pwd;
    public String execute(){
        if("siggy".equals(name)&&"111".equals(pwd)){
            return "success";
        }
        else {
            return "failed";
        }
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }


}

6 .在struts.xml中配置LoginAction

<?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>
    <package  name="default"  namespace="/" extends="struts-default">
        <action name="login"   class="cn.sxt.siggy.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="failed">/login.jsp</result>
        </action>
    </package> 
</struts>

上述jsp中pageEncoding设置为UTF-8,不然会出现乱码

调试记载:

action的提交地址.action是扩展名,默认为.action,action的扩展名和web.xml的url-pattern要相匹配。web.xml里面的url-pattern可以写成*.action或者/*,但是如果没有修改过,写成其他都不可以,查看这里写图片描述
可以看到struts.action.extension=action,,,如果需要修改真的需要其他扩展名,那么可以在这里修改,但是直接修改源代码是十分不好的选择,强烈不推荐,在不修改源代码的情况下有两种方法修改。

(1) web.xml

给filter加个init-param

<init-param>  
<param-name>struts.action.extension</param-name>  
<param-value>do</param-value>  
</init-param>  

(2)struts.xml

<constant name="struts.action.extension" value="do" />

尽管这样,但是一般来说Struts2的默认够用了,推荐使用*.action

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值