当前位置: 移动技术网 > IT编程>开发语言>Java > Struts2开发环境搭建 附简单登录功能实例

Struts2开发环境搭建 附简单登录功能实例

2019年07月22日  | 移动技术网IT编程  | 我要评论
首先是搭建struts2环境。 第一步 下载struts2 去struts官网 下载struts2组件。 截至目前,struts2最新版本为2.3.1.3,下载st

首先是搭建struts2环境。

第一步 下载struts2
去struts官网 下载struts2组件。
截至目前,struts2最新版本为2.3.1.3,下载struts-2.3.16.3-all.zip,解压,放着。

第二步 新建web project并导入jar包
在myeclispe中新建web project,然后找到解压的struts2包,在里面apps文件夹下找到struts2-blank.war,解压这个war文件,将里面web-inf\lib目录下的jar文件全部复制到新建的web project的webroot\web-inf\lib目录下。

第三步 配置web.xml
在项目的webroot\web-inf\目录下找到web.xml文件,没有就新建一个web.xml文件,在里面添加如下代码:

<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.xml文件的示例:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 id="webapp_id" version="3.1">
 <display-name>web1</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></welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
</web-app>

第四步 配置struts.xml
在项目的src目录下找到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="main" extends="struts-default">
  <!-- 在这里面配置action -->
 </package>
</struts>

到此,struts2开发环境搭建完成。

下面演示一个登录页面实例

第一步 修改index.jsp
修改index.jsp,做出登录界面。
下面是index.jsp的代码:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>

<!doctype html>
<html>
 <head>
 <title>登录</title>
 </head>
 
 <body>
 <form action="login" method="post">
 登录<br />
 账号:<input type="text" name="username" /><br />
 密码:<input type="password" name="password" /><br />
 <input type="submit" value="登录" />
 </form>
 </body>
</html>

下面是index.jsp在浏览器中的效果:

第二步 编写验证账户和密码的类
新建logaction类,让其继承com.opensymphony.xwork2.actionsupport类,注意到index.jsp中两个输入框的name属性分别是username和password,所以logaction类必须包含下面两个属性:
private string username
private string password

而且必须写出他们的get、set方法。

然后,编写execute方法,在execute方法中验证账号和密码并返回string类型的结果,execute方法会在该action类被调用的时候自动执行。

下面是logaction.java的完整代码:

package com.lidi.struts.action;

import com.opensymphony.xwork2.actionsupport;

public class logaction extends actionsupport {

 private static final long serialversionuid = 1l;
 private string username;//账号
 private string password;//密码
 
 //getters & setters
 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;
 }
 
 /**
  * execute方法会在该action类被调用的时候自动执行,
  * 如果 账号="admin"并且密码="123456",就返回success
  * 否则返回error
  */
 public string execute(){
  if(username.equalsignorecase("admin") && password.equalsignorecase("123456")){
   return success;
  }
  else
   return error;
 }
}

上面的返回success和返回error什么意思呢,后面再讲。

第三步 配置struts.xml
第二步编写了action类,第三步将该action配置到struts.xml中,打开struts.xml,在package标签中添加如下代码:

<action name="login" class="com.lidi.struts.action.logaction">
 <result name="success">success.jsp</result>
 <result name="error">error.jsp</result>
</action>

action标签的name属性为login,这个必须与index.jsp中form标签的action属性一致,class属性填写logaction类的全称。

<result name="success">success.jsp</result> 这个标签的意思是当logaction类的execute方法返回success时,页面跳转到success.jsp;同理,<result name="success">success.jsp</result> 这个标签的意思是当logaction类的execute方法返回error时,页面跳转到error.jsp。

完整的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="main" extends="struts-default">
  <action name="login" class="com.lidi.struts.action.logaction">
   <result name="success">success.jsp</result>
   <result name="error">error.jsp</result>
  </action>
 </package>

</struts>

这里用到了success.jsp和error.jsp,在项目中新建这两个文件,success.jsp表示登录成功后的页面,上面显示登录用的账户和密码,error.jsp表示登录失败后的页面,上面显示错误提示就就好了,他们的代码分别如下:
success.jsp

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!doctype html>
<html>
 <head>
 <title>登陆成功</title>
 </head>
 
 <body>
 欢迎<s:property value="username" />,登录成功!<br />
 </body>
</html>

<%@ taglib prefix="s" uri="/struts-tags"%>表示引用struts标签库
<s:property value="username" />是struts标签,用以显示登录页面传递过来的账号。

error.jsp

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>

<!doctype html>
<html>
 <head>
 <title>登录失败</title>
 </head>
 
 <body>
 登录失败!用户名或密码错误!
 </body>
</html>

第四步 运行
配置struts.xml后要重启下服务器,然后在浏览器中查看效果。
分别输入账号和密码并登录,如果账号和密码分别为admin和123456,页面就会显示
欢迎admin,登录成功!
否则就会显示
登录失败!用户名或密码错误!

第五步 程序运行原理浅析
用户填写账号密码并点击登录后,浏览器会请求form标签action属性里面的链接,也就是login。服务器中,过滤器拦截到login这个请求,就会查找struts.xml中name=login的action,再找到这个action的class属性对应的类,也就是com.lidi.struts.action.logaction,然后实例化一个logaction对象,并且把参数username和password分别赋给这个对象的username和passwrod属性(这就是为什么logaction类的两个属性名称必须和index.jsp中两个文本框的name属性分别相同,并且必须添加他们的get和set方法),然后执行这个对象的execute方法,并返回一个字符串,如果返回success字符串,就查找struts.xml中对应action的<result>标签中name属性等于success的<result>标签,并将页面转到该标签里面配置的页面success.jsp上。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网