Struts2——登陆初步(入门)

学习Struts2最简单的一个实例便是注册登陆了,现在就从这个开始不断地学习Struts2吧!!!</span>

首先给大家看一下目录结构——


是不是很简单啊???

(1)建立一个web工程【Dynamic Web Project】


一路【Next】就好了......

(2)添加【lib】


看好路径,将全部的 .jar 文件复制到【WebContent/WEB-INF/lib】下


(3)配置【web.xml】中的struts


在【blank】文件夹下找到【web.xml】——>打开


复制红框中的信息到项目中【web.xml】


(4)开始写Java程序

我直接贴代码了

package com.ben.struts2.submit;

public class User {

	private String userName;
	private Integer userAge;
	private String password;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getUserAge() {
		return userAge;
	}

	public void setUserAge(Integer userAge) {
		this.userAge = userAge;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [userName=" + userName + ", userAge=" + userAge
				+ ", passworld=" + password + "]";
	}

	public String save() {
		System.out.println("save = " + this);
		return "save";
	}

}

(5)struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="submit" extends="struts-default" >
    
    <action name="user-input" class="com.opensymphony.xwork2.ActionSupport"
			method="execute" >
			<result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
			</action>
    
    <action name="user-save" class="com.ben.struts2.submit.User"
			method="save">
			<result name="save">/WEB-INF/pages/success.jsp</result>
			</action>
    
    </package>

</struts>

(6)jsp文件
index.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>

	<a href="user-input.action">点击注册</a>

</body>
</html>
input.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>

	请填写完成注册信息
	<form action="user-save.action" method="post">
		用户名:<input type="text" name="userName" /> 
		<br> <br>
		密码:<input type="text" name="password" /> 
		<br> <br> 
		年龄:<input type="text" name="userAge" /> 
		<br> <br>
	<input type="submit" value="submit"/>
	</form>
</body>
</html>

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>
以下是你的注册信息
<br>
用户名:${userName }
<br>
密码:${password }
<br>
年龄:${userAge }

</body>
</html>

好了,大家先跑前来试一试吧!!!




发布了38 篇原创文章 · 获赞 15 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/u013057271/article/details/39185797