项目开发---实现注册功能

接下就要用到Struts框架了,再用之前先配置好有关操作

1.在web.xml设置前端配置器

2.在src下新建struts.xml

3.写好首页jsp:

4.配置好struts

5.开发好action类

1.在web.xml设置前端配置器

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name></display-name> <!-- 配置Struts2的前端控制器 -->
<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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

2.在src下新建struts.xml

3.首页jsp:

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>用户信息管理</title>
</head> <body style="text-align: center;">
<h4>用户信息管理</h4>
<a href="${pageContext.request.contextPath }/user_addui.action" target="body">注册用户</a>
<a href="${pageContext.request.contextPath }/user_list.action" target="body">用户列表</a>
<!-- 这里 user_addui这样命名是有很大意义的,是让struts.xml配置的可以用通配符,接下来会讲-->
</body>
</html>

图片实例:

4.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>
<!-- 配置常量信息 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 注意到没 user_*,后面的{1}就是代表前面的内容,前面如果是addui那后面就执行addui方法,这样就可以一个action实现多个方法-->
<package name="struts2" extends="struts-default" >
<action name="user_*" class="com.study.user.action.UserAction" method="{1}">
<result name="uiSUCCESS" type="dispatcher">/jsp/register.jsp</result>
<result name="addSUCCESS" type="redirectAction">user_list</result>
<!--redirectAction :redirectAction将请求重定向到另一个action等于在运行 <action name="user_list"
class="com.study.user.action.UserAction" method="list">,注册好后直接显示所有用户信息 -->
<result name="listSUCESS" type="dispatcher">/jsp/listuser.jsp</result>
<result name="delSUCESS" type="dispatcher">/jsp/listuser.jsp</result>
<result name="updateSUCESS" type="dispatcher">/jsp/register.jsp</result>
<!--uiSUCCESS调到的是一个注册界面, addSUCCESS当到注册界面当你填完信息后,点击注册把数据保存到数据库,在跳转到显示所有产品界面, -->
<!--listSUCESS到了用户列表界面,显示所有用户信息。delSUCESS删除信息后还是跳转到显示所有用户界面。
updateSUCESS更新的时候跳转到注册界面并且回显数据 -->
</action>
</package>
</struts>

 5.开发好aciton类 这个和servlet有个很大的好处就是能够实现一个action可以实现多个方法

 import java.util.List;
import java.util.UUID; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.study.user.model.User;
import com.study.user.service.UserService;
import com.study.user.service.impl.UserServiceImpl;
import com.study.user.utils.Globals; public class UserAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); private UserService service =new UserServiceImpl(); public User getModel() {
return user;
}
/*
* 注册界面
*/
public String addui() throws Exception { // 模拟读取配置文件 设置信息
//因为我的兴趣爱好的类型不是写在jsp界面的,而是写在java类中,目的也是为了后期更好的维护,所以要把它们返回的jsp界面
ServletActionContext.getRequest().setAttribute("preferences", Globals.preferences);
ServletActionContext.getRequest().setAttribute("types", Globals.types); return "uiSUCCESS";
}
/*
* 把对象保存到数据库中
*/
public String add() throws Exception {
//通过uuid新建一个自动创建一个id
user.setId(UUID.randomUUID().toString());
service.addUser(user); return "addSUCCESS"; }
/*
* 回显所有用户信息
*/
public String list() throws Exception{
List<User> users =service.getAll(); ServletActionContext.getRequest().setAttribute("users", users);
return "listSUCESS";
}
/*
* 删除某一个用户
*/
public String del() throws Exception{
String id= ServletActionContext.getRequest().getParameter("id");
service.del(id); //这里是删除掉这条用户
List<User> users =service.getAll(); //下面是为了把剩余的还显示在当前页面
ServletActionContext.getRequest().setAttribute("users", users);
return "delSUCESS";
}
/*
* 更新用户
*/
public String update() throws Exception{
//先通过id获得user对象
String id= ServletActionContext.getRequest().getParameter("id");
User user=service.find(id);
//再把对象转发到注册界面用于回显信息
ServletActionContext.getRequest().setAttribute("user", user);
//因为兴趣爱好也是写在java类中,所以需要在放到域中,用于回显
ServletActionContext.getRequest().setAttribute("preferences", Globals.preferences);
ServletActionContext.getRequest().setAttribute("types", Globals.types);
return "updateSUCESS";
}
}

最新文章

  1. linux配置的问题
  2. Oracle 6 - 锁和闩 - 并发问题和隔离级别
  3. 天草(初级+中级+高级)VIP和黑鹰VIP破解教程(全部iso下载地址)
  4. [Excel] CsvHelper---C#关于CSV文件的导入和导出以及转化 (转载)
  5. MySQL中的类Decode用法
  6. C++工厂方法模式
  7. 设为首页 收藏(IE可用)
  8. After a rest, go on
  9. C# 反射、与dynamic最佳组合
  10. angular4.0微信oAuth第三方认证的正确方式
  11. Linux下源码安装并配置Nginx
  12. wepy框架自定义组件编译报错not Found File XXX.wxss
  13. appium手机键盘实现方法
  14. IO流-学习使人快乐2
  15. asp.net C#压缩打包文件例子
  16. centos7下安装gcc7
  17. 自动化CodeReview - ASP.NET Core依赖注入
  18. Oracle 11g EM删除重建的方法
  19. Angular4.+ ngx-bootstrap Pagination 自定义分页组件
  20. GRYZ 模 拟 赛 系 列 Xxy 的车厢调度

热门文章

  1. 【Java疑难杂症】有return的情况下try catch finally的执行顺序
  2. SQLServer2008数据库连接error40错误
  3. VueJS引入css或者less文件的一些坑
  4. Java学习笔记13---如何理解“子类重写父类方法时,返回值若为类类型,则必须与父类返回值类型相同或为其子类”
  5. app打包常用操作
  6. 【二十】mysqli基于面向过程与面向对象的编程
  7. Qt creator中文输入—fctix-qt5 源码编译 libfcitxplatforminputcontextplugin.so
  8. ACM HDU 1081 To The Max
  9. Java数据结构和算法(十)——二叉树
  10. 基于 Vue.js 的移动端组件库mint-ui实现无限滚动加载更多