首页 > 代码库 > 6.Struts2简单类型数据的接受

6.Struts2简单类型数据的接受

简单类型数据的接收
在Action类中定义与请求参数同名的属性,
即,要定义该属性的set方法,便能够使struts2自动接收请求参数并赋予同名属性。

简单类型数据的接受举例:

新建工程项目,名称为:receive_simple_params。

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,输入地址:

http://127.0.0.1:8080/receive_simple_params/

运行截图如下:


 


 

 

6.Struts2简单类型数据的接受