© 版权声明:本文为博主原创文章,转载请注明出处

接收参数

  - 使用Action的属性接收参数

  - 使用Domain Model接收参数

  - 使用ModelDriven接收参数

实例

1.项目结构

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.struts</groupId>
<artifactId>Struts2-Parameter</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Struts2-Parameter Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<struts.version>2.5.10</struts.version>
</properties> <dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<finalName>Struts2-Parameter</finalName>
</build>
</project>

3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.0" metadata-complete="true"> <welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> <!-- struts2接入 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

4.User.java

package org.struts.model;

public class User {

	private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

5.LoginAction.java

  5.1 方式一:使用Action的属性接收参数

package org.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String login() { System.out.println("username = " + username);
System.out.println("password = " + password);
return SUCCESS; } }

  5.2 方式二:使用Domain Model接收参数

package org.struts.action;

import org.struts.model.User;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private User user;

	public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public String login() { System.out.println("username = " + user.getUsername());
System.out.println("password = " + user.getPassword());
return SUCCESS; } }

  5.3 方式三:使用ModelDriven接收参数

package org.struts.action;

import org.struts.model.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class LoginAction extends ActionSupport implements ModelDriven<User> { private static final long serialVersionUID = 1L; private User user = new User(); public User getModel() {
return user;
} public String login() { System.out.println("username = " + user.getUsername());
System.out.println("password = " + user.getPassword());
return SUCCESS; } }

6.struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts> <package name="login" extends="struts-default" namespace="/">
<action name="login" class="org.struts.action.LoginAction" method="login">
<result>/success.jsp</result>
</action>
</package> </struts>

7.login.jsp

  7.1 方式一和方式三

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="登录">
<input type="reset" value="重置">
</form>
</body>
</html>

  7.2 方式二

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="user.username"/><br/>
密码:<input type="password" name="user.password"/><br/>
<input type="submit" value="登录">
<input type="reset" value="重置">
</form>
</body>
</html>

8.success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功界面</title>
</head>
<body>
登录成功
</body>
</html>

9.效果预览

  

Struts2学习八----------接收参数的更多相关文章

  1. Struts2中Action接收参数的四种形式

    1.Struts2的Action接收参数的三种形式.      a. 使用Action的属性接收(直接在action中利用get方法来接收参数):                   login.js ...

  2. Struts2中Action接收参数的方法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt112 Struts2中Action接收参数的方法主要有以下三种: 1.使用A ...

  3. Struts2中Action接收参数的方法主要有以下三种:

    Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数(最原始的方式):     a.定义:在Action类中定义属性,创建get和set方法:     b.接 ...

  4. Struts2中Action接收参数

    Struts2中Action接收参数的方法主要有以下三种: Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数:     a.定义:在Action类中定义属 ...

  5. struts2属性Struts2中属性接收参数中文问题和简单数据验证

    PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 一:如果表单提交数据中有中文时,尽量应用post方式. 需要在Struts. ...

  6. 学习笔记之Struts2—浅析接收参数

    最近自己通过视频与相关书籍的学习,对action里面接收参数做一些总结与自己的理解. 0.0.接收参数的(主要)方法   使用Action的属性接收参数 使用DomainModel接收参数 使用Mod ...

  7. Struts2学习---基本配置,action,动态方法调用,action接收参数

    首先我们先来直接配置,然后再来讲原理: 第一步:jar包的引入: 我们可以到struts2的官网上下载: http://struts.apache.org/download.cgi#struts251 ...

  8. struts2学习笔记(4)接收参数

    ①用action属性接收 登录界面例子 在webroot下创建login.jsp和success.jsp login.jsp中加入表单: <form action="LoginActi ...

  9. Struts2 DomainModel、ModelDriven接收参数

    一.DomainModel(域模型) 1. 应用场景:一般我们在struts2的action中接收参数通常是如下方式 package cn.orlion.user; import com.opensy ...

随机推荐

  1. vue中通过路由跳转的三种方式

    原文:https://blog.csdn.net/qq_40072782/article/details/82533477 router-view 实现路由内容的地方,引入组件时写到需要引入的地方需要 ...

  2. ToolTip特效 JavaScript 盗取厦门人才网的特效

    原文发布时间为:2009-05-17 -- 来源于本人的百度文章 [由搬家工具导入] 源代码:http://www.xmaspx.com/Services/FileAttachment.ashx?At ...

  3. 使用vim修改和查看16进制文件

    使用前的准备工作,如果没有安装,使用命令安装: pacman -S vim 使用vim的十六进制功能查看和编辑文本文件,创建测试文件,使用命令如下: vim test.txt 进入“插入”模式,使用命 ...

  4. C++学习(二):学会使用stringstream

    1.前言 今天在CppTemplateTutorial群里,有人问了一个问题:这一堆add怎么简化掉 https://wandbox.org/permlink/vDPDwMFbBIQSSymS.代码如 ...

  5. C# 的 String.CompareTo Equals和==的比较

    String.CompareTo 语法 public int CompareTo(    string strB) 返回值 小于 0,实例小于参数 strB: 0,实例等于参数 strB: 大于 0, ...

  6. ubuntu16.04安装docker CE

    如需开始在 Ubuntu 上使用 Docker CE,请确保您满足先决条件,然后再安装 Docker. 如需安装 Docker 企业版 (Docker EE),请转至获取适用于 Ubuntu 的 Do ...

  7. springBoot 环境

    环境约束 jdk1.8:Spring Boot 推荐jdk1.7及以上:maven3.x:maven 3.3以上版本:Apache Maven 3.3.9.IntelliJIDEA2017:Intel ...

  8. java 修改字体大小

    在Windows->Preferences->General->Appearance->Colors and Fonts->Java->Java Editor Te ...

  9. 如何将Windows8系统的磁盘格式(GPT格式)转换成Windows 7系统的磁盘格式(MBR格式)

    知识点分析:随机预装Win8的电脑,磁盘为GPT格式的,如果需要安装Win7等早期版本系统,需要转换为MBR格式的,使用Diskpart命令即可完成转换.操作步骤: 注意:转换磁盘格式需要清空磁盘中的 ...

  10. JS-JavaScript String 对象-string对象方法3:concat()

    1.concat():用于连接两个或多个字符串. 1).语法:string.concat(string1, string2, ..., stringX)    (string1, string2, . ...