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;
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.jsp页面,
注意:本例在success.jsp中使用struts2标签接收
<%@taglib uri="/struts-tags" prefix="s" %>
<s:property value="message"/>
否则跳转到error.jsp:
3、完整源码:
链接:indexStruts2
提取码:22a7