Struts2学习八----------接收参数
© 版权声明:本文为博主原创文章,转载请注明出处
接收参数
- 使用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.效果预览
© 版权声明:本文为博主原创文章,转载请注明出处