Struts2运用基本JavaBean

web.xml中的内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>mystruts2learn</display-name>
  <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>
</web-app>

然后是struts.xml

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



	<package name="login" extends="struts-default">
		<action name="showloginpage" class="com.mystruts2.login.showloginpage">
			<result name="success" type="dispatcher">/loginpage.jsp</result>

		</action>
		<action name="login" class="com.mystruts2.login.login">
			<result name="loginerror" type="dispatcher">/loginpage.jsp</result>
			<result name="loginsuccess" type="dispatcher">/success.jsp</result>
		</action>
		
		<action name="author" class="com.mystruts2.admin.author">
			<result name="success" type="dispatcher">/loginpage.jsp</result>
			
		</action>
	</package>



</struts>    

user.java的javabean

package com.mystruts2.login;

public class user {
	private String username;
	private String password;
	
	public void  user(String name,String pwd)
	{
		this.password = pwd;
		this.username = name;
	}
	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;
	}

}

login.java来进行判断

package com.mystruts2.login;

import com.opensymphony.xwork2.ActionSupport;

public class login extends ActionSupport {
	user u ;
	
	public user getU() {
		return u;
	}

	public void setU(user u) {
		this.u = u;
	}

	public String execute() {
		// Empty username or password value is not permitted.

		System.out.println(u.getUsername());
		System.out.println(u.getPassword());
		
		
		if (u.getUsername().equals("") || u.getPassword().equals("")) {
			addActionError("Invalid username or password. Please try again!");
			return "loginerror";
		}
		else if(u.getUsername().equals("test")&& u.getPassword().equals("test"))
			return "loginsuccess";
		else{
			addActionError(u.getUsername() + " 或者 "+ u.getPassword()+"验证失败");
			return "loginerror";
		}
	}
}

showloginpage.java来进行跳转

package com.mystruts2.login;

import com.opensymphony.xwork2.ActionSupport;

public class showloginpage extends ActionSupport {

	public String execute() {
		return "success";
	}
}

然后就是loginpage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'loginpage.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<center>
		<h1>
			<s:actionerror />
		</h1>
		<form action="login.action" method="post">
			用户名<input type="text" name="u.username"><br> 密 码<input
				type="password" name="u.password"><br> <input
				type="submit">
		</form>
	</center>
</body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h1>Welcome Back!</h1>
    <s:property value = "u.username "/><td>
    <s:property value = "u.password" /><td>
  </body>
</html>

发表评论

电子邮件地址不会被公开。 必填项已用*标注