• Intellij IDEA《十分钟,配置struts2》by me


    1.加载Struts 2类库

    <dependencies>
    <!-- Struts 2 核心包-->
    <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.3.4.1</version>
    </dependency>
    <!-- Struts 2 构建基础-->
    <dependency>
    <groupId>org.apache.struts.xwork</groupId>
    <artifactId>xwork-core</artifactId>
    <version>2.3.4.1</version>
    </dependency>
    </dependencies>
    <build>
    <resources>
    <resource>
    <directory>src/main/java</directory>
    <includes>
    <include>**/*.xml</include>
    </includes>
    </resource>
    </resources>
    </build>

    2.配置wbe.xml文件

    <web-app>
    <display-name>Archetype Created Web Application</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>

    </web-app>
     

    3.开发视图层页面

    <%@ taglib uri="/struts-tags" prefix="s"   %>
    <html>
    <head>
    <title>Struts</title>
    </head>
    <body>
    <h2>第一个Strusts 2案例</h2>
    <s:property value="message"></s:property>
    <form action="helloWorld" method="post">
    用户名:<input name="uname" /><s:fielderror fieldName="uname"></s:fielderror>
    密码: <input type="password" name="upwd"/><s:fielderror fieldName="pwd" ></s:fielderror>
    <input type="submit" value="提交">
    </form>
    </body>
    </html>
     

    4.开发控制层页面Action

    public class HelloWorldAction extends ActionSupport implements Action {
    private String uname;
    private String upwd;
    private String message;
    //唯一的执行方法
    public String execute() throws Exception {
    if (this.getUname().equals("1")&&this.getUpwd().equals("1")){
    this.setMessage("Hello!"+this.getUname());
    return "success";
    }else{
    return "input";
    }

    }
    //服务器端数据校验
    public void validate(){
    if (this.getUname().length()==0){
    addFieldError("uname","用户名不能为空");
    }
    if (this.getUpwd().length()==0){
    addFieldError("pwd","密码不能为空");
    }
    }

    public String getUpwd() {
    return upwd;
    }

    public void setUpwd(String upwd) {
    this.upwd = upwd;
    }

    public String getUname() {
    return uname;
    }

    public void setUname(String uname) {
    this.uname = uname;
    }

    public String getMessage() {
    return message;
    }

    public void setMessage(String message) {
    this.message = message;
    }

    }

    5.配置Struts.xml

    <struts>
    <package name="default" namespace="/" extends="struts-default">
    <action name="helloWorld" class="HelloWorldAction">
    <result name="success">/First.jsp</result>
    <result name="input">/First.jsp</result>
    </action>
    </package>
    </struts>

    6.部署、运行项目

      

    • 直接运行

    • 正确输入用户名,密码

    
    
     
  • 相关阅读:
    Oracle DB 管理数据库的空间
    五 Django 1.5.4 User Authentication 用户认证
    接口开放在即,先学招行做好微信内容营销
    WPF的ItemsControl设置数据源以及Binding使用
    WPF中后台代码停止RepeatBehavior=RepeatBehavior.Forever的办法
    WPF中使用RenderTransformOrigin来控制动画的起点
    使用Rectangle+ImageBrush来代替Image,解决图片模糊的问题
    WPF中Style文件引用另一个Style文件中的样式
    WPF中用后台C#代码为TabItem设置Background属性
    WPF中设置Border的BorderThickness属性会让背景图片产生模糊感
  • 原文地址:https://www.cnblogs.com/xlht/p/6478964.html
Copyright © 2020-2023  润新知