您的位置:首页 > 编程语言 > Java开发

struts学习之用户登录

2016-03-23 17:32 225 查看
整体过程为:

1、新建dynamical web project工程

2、拷贝jar包到WEB-INF lib文件夹下

3、web.xml文件添加核心过滤器拦截用户请求

4、定义jsp文件(.jsp)

5、定义处理用户的Action类(.java)

6、配置Action(struts.xml)

7、配置处理结果与物理视图资源之间的对应关系

(Action.java处理用户请求结束后,会返回一个处理结果,通常是字符串;)

8、编写对应的视图资源(相应跳转的.jsp文件)

具体如下:

1、在java EE下新建dynamical web project工程

2、拷贝jar包到WEB-INF lib文件夹下

jar包为...\struts-2.3.24.1-all\struts-2.3.24.1\apps 中的struts2-blank.war,WEB-INF lib文件夹中所有jar包

3、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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>login</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>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
</web-app>


4、新建login.jsp

设置action name为login,方法为post,默认方法为get,会在url中显示用户的用户名和密码——不安全;

该jsp文件会经过web.xml中的过滤器过滤,得到该action传来的参数name和password

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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=GBK">
<title><s:text name="loginPage"/></title>
</head>
<body>
<h2>please login!</h2>
<center>
<form action="login" method="post">
name:<input type="text" name="name"/><br/>
password:<input type="password" name="password"/><br/>
<input type="submit" value="submit"/>
</form>
</center>
</body>
</html>


5、新建LoginAction.java

设置字段name和password,与jsp中的变量名相匹配。

默认执行execute方法:用户通过login.jsp提交的name和password被捕获,并提交给LoginAction中的Execute方法,该name和password与指定的用户名和密码进行匹配,如果匹配返回“success”结果字段,或者返回“error”结果字段——这时与struts.xml中的result相匹配,并调到相应的jsp页面。

package testWeb;

public class LoginAction {
private String name;
private String password;

public String execute() throws Exception{
if(getName().equals("zhaod")
&&getPassword().equals("zhaod")){
return "success";
}else{
return "input";
}

}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}


6、struts.xml

指定package;

指定action的name,以及相关联的类:注意类名前需要有相应的包名,用.隔开;

获取该action的result,并根据Action的Execute方法返回的结果,调到相应的jsp页面

<?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="testWeb" namespace="/" extends="struts-default">
<action name="login" class="testWeb.LoginAction">
<result name="input">/login.jsp</result>
<result name="error">/login.jsp</result>
<result name="success">/WEB-INF/content/welcome.jsp</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>

<!-- Add packages here -->

</struts>


实现效果为:

用户输入用户名和密码,只有当输入的用户名和密码与指定的用户名密码匹配时,才调到欢迎页面。

整体的框架为:



其中:login.jsp在WebContent下

welcome.jsp在WEB-INF/content下

web.xml在WEB-INF下

LoginAction.java在src/testWeb下

struts.xml在src下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: