Strut2页面跳转 --Struts2

1、本案例借助struts2框架,完成页面跳转功能

2、代码实现

index.jsp:

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

  <div class="form-group">

     <label for="">用户名:</label>

     <input type="text" class="form-control" name="message">
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

struts2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <filter>
    <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

HelloStruts2.java

package cn.thanlon.www.action;

扫描二维码关注公众号,回复: 5502499 查看本文章

public class HelloStruts2 {
  private String message;
  public String getMessage(){
    return message;
  }
  public void setMessage(String message){
    this.message =message;
  }
  public String execute(){
    if(getMessage().isEmpty())
      return "error";
    else
      return "success";
  }
}

如果表单中提交数据,则跳转到success.jps页面,

 

注意:本例在success.jsp中使用struts2标签接收

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

<s:property value="message"/>

否则跳转到error.jsp

3、完整源码:

链接:indexStruts2
提取码:22a7 

猜你喜欢

转载自www.cnblogs.com/qikeyishu/p/10515826.html
今日推荐