使用Struts2框架,实现用户注册、登录和修改密码三个功能

 

使用Struts2框架,实现用户注册、登录和修改密码三个功能,自行设计数据库中的用户表(UserInfo),要求注册成功后在一个新页面显示用户全部注册信息;登录成功后将用户ID存储到session中并在一个新页面显示欢迎信息;修改密码成功后自动跳转至登录页面。

使用Struts2框架,实现用户注册、登录和修改密码三个功能:

Struts2框架:

https://img-blog.csdnimg.cn/20181219211414182

其中dto包是存放数据的, DAO都是进行数据操作类, 是对于数据库中数据做增删改查等操作代码,然后util包是进行数据库的链接。

 

Action是响应事件的,这是struts的一个特色。我这里面servlet是JavaScript+javaBean+servlet。就是没有使用到框架。

 

 配置好struts2文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <!--设置post方法默认的编码方式为UTF-8-->

    <constant name = "struts.i18n.encoding" value = "utf-8"></constant>

    <!--设置struts.xml改变后自动加载-->

    <constant name = "struts.configuration.xml.reload" value = "true"/>

    <!--设置浏览器不加载缓存,以免影响到开发-->

    <constant name = "struts.serve.static.broswerCache" value = "false"/>

    <!--设置当前模式为开发模式,会在报错时,有更加详细的提示信息-->

    <constant name = "struts.devMode" value = "true"/>

    <constant name="struts.multipart.saveDir" value="/tmp"/>

    <package name = "default" extends = "struts-default">

        <action name = "userLogin" class = "com.jinhong.test2.action.LoginAction" >   //其中actionname对应的是你响应的事件,一般就是你的form表单。

            <result name = "success">/success.jsp</result>   //成功就跳到成功

            <result name = "error">/error.jsp</result>

        </action>

       

        <action name = "registUser" class = "com.jinhong.test2.action.LoginAction" method="regist" >  //使用method是因为这个不是action类里面的默认函数,是你重新定义的。

            <result name = "success">/success.jsp</result>

            <result name = "error">/error.jsp</result>

        </action>

       

        <action name = "changeUser" class = "com.jinhong.test2.action.LoginAction" method="change" >

            <result name = "success">/success.jsp</result>

            <result name = "error">/error.jsp</result>

        </action>

       

        <action name = "printUser" class = "com.jinhong.test2.action.LoginAction" method="printname" >

            <result name = "success">/success.jsp</result>

            <result name = "error">/error.jsp</result>

        </action>

    </package>

</struts>

 

Web.xml文件的配置

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

    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">

  <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>

 

  3.对数据库的操作

 

4.  登录界面的代码

 

其中标签里面的name字段必须和action里面的字段要相同

 

a)基本属性注入(页面,Action)

https://img-blog.csdnimg.cn/20181219211414213

 

b)域模型注入(页面,Action)

    https://img-blog.csdnimg.cn/20181219211414237

 

 

2.页面从Action取值

    a)使用EL表达式

     https://img-blog.csdnimg.cn/20181219211414256

 

猜你喜欢

转载自blog.csdn.net/hujinhong145/article/details/85108398