一、本案例藉助struts2框架,完成頁面傳參、跳轉功能
二、代碼實現
index.jsp:java
<form action="helloStruts2.action" method="post">web
<div class="form-group">apache
<label for="">用戶名:</label>app
<input type="text" class="form-control" name="message">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>框架
struts2.xml:jsp
<?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>post
HelloStruts2.java:this
package cn.thanlon.www.action;url
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";
}
}spa
若是表單中提交數據,則跳轉到success.jsp頁面,
注意:本例在success.jsp中使用struts2標籤接收
<%@taglib uri="/struts-tags" prefix="s" %>
<s:property value="message"/>
不然跳轉到error.jsp:
三、完整源碼:
連接:indexStruts2
提取碼:22a7