简单类型数据的接受举例:
新建工程项目,名称为:receive_simple_params。
加载struts的核心jar包
Simple_Params_Action.java源码如下:
package actions; public class Simple_Params_Action {
private String username;
private int age; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} public String execute(){
System.out.println("---------username=---------"+username);
System.out.println("---------age=---------"+age);
return "success";
} }
index.jsp页面源码如下:
<%@ page pageEncoding="UTF-8"%>
<html>
<head> <title>注册页面</title> </head> <body>
<form action="simple.action" method="post">
用户名<input type="text" name="username"/><br/>
年龄<input type="text" name="age"/></br>
<input type="submit" value="注册"/>
</form>
</body>
</html>
welcome.jsp源码如下:
<%@ page pageEncoding="utf-8" isELIgnored="false"%> <html>
<head> <title>welcome page</title> </head> <body>
用户名:${username}<br/>
年龄:${age} </body>
</html>
struts.xml源码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="one" extends="struts-default"> <action name="simple" class="actions.Simple_Params_Action">
<result>/welcome.jsp</result>
</action> </package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd"> <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> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
部署发布,启动tomcat,输入地址:
运行截图如下: