项目(1)----用户信息管理系统(4)---(struts开发)

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

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

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

         2.在src下新建struts.xml

         3.写好首页jsp:

         4.配置好struts

         5.开发好action类

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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <display-name></display-name>    
 8   
 9    <!-- 配置Struts2的前端控制器 -->
10    <filter>
11       <filter-name>struts2</filter-name>
12       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
13   </filter>
14   
15   <filter-mapping>
16       <filter-name>struts2</filter-name>
17       <url-pattern>/*</url-pattern>
18   </filter-mapping>
19   
20   <welcome-file-list>
21     <welcome-file>index.jsp</welcome-file>
22   </welcome-file-list>
23 </web-app>

2.在src下新建struts.xml

3.首页jsp:

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

图片实例:

4.struts.xml

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>  
 6       <!-- 配置常量信息 -->
 7       <constant name="struts.devMode" value="true"></constant> 
 8       <!-- 注意到没 user_*,后面的{1}就是代表前面的内容,前面如果是addui那后面就执行addui方法,这样就可以一个action实现多个方法-->
 9      <package name="struts2" extends="struts-default" >
10          <action name="user_*" class="com.study.user.action.UserAction" method="{1}">
11             <result name="uiSUCCESS"  type="dispatcher">/jsp/register.jsp</result>
12             <result name="addSUCCESS" type="redirectAction">user_list</result>
13             <!--redirectAction :redirectAction将请求重定向到另一个action等于在运行 <action name="user_list" 
14                class="com.study.user.action.UserAction" method="list">,注册好后直接显示所有用户信息 -->
15             <result name="listSUCESS" type="dispatcher">/jsp/listuser.jsp</result>
16             <result name="delSUCESS" type="dispatcher">/jsp/listuser.jsp</result>
17             <result name="updateSUCESS" type="dispatcher">/jsp/register.jsp</result>
18            <!--uiSUCCESS调到的是一个注册界面, addSUCCESS当到注册界面当你填完信息后,点击注册把数据保存到数据库,在跳转到显示所有产品界面, -->
19            <!--listSUCESS到了用户列表界面,显示所有用户信息。delSUCESS删除信息后还是跳转到显示所有用户界面。
20               updateSUCESS更新的时候跳转到注册界面并且回显数据 -->
21          </action>     
22      </package>
23 </struts>

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

 1 import java.util.List;
 2 import java.util.UUID;
 3 
 4 import org.apache.struts2.ServletActionContext;
 5 
 6 import com.opensymphony.xwork2.ActionSupport;
 7 import com.opensymphony.xwork2.ModelDriven;
 8 import com.study.user.model.User;
 9 import com.study.user.service.UserService;
10 import com.study.user.service.impl.UserServiceImpl;
11 import com.study.user.utils.Globals;
12 
13 public class UserAction extends ActionSupport implements ModelDriven<User> {
14 
15     private User user = new User();
16     
17     private  UserService service =new UserServiceImpl();
18 
19     public User getModel() {
20         return user;
21     }
22     /*
23      * 注册界面
24      */
25     public String addui() throws Exception {
26         
27         // 模拟读取配置文件 设置信息
28         //因为我的兴趣爱好的类型不是写在jsp界面的,而是写在java类中,目的也是为了后期更好的维护,所以要把它们返回的jsp界面
29         ServletActionContext.getRequest().setAttribute("preferences", Globals.preferences);
30         ServletActionContext.getRequest().setAttribute("types", Globals.types);
31 
32         return "uiSUCCESS";
33     }
34    /*
35     * 把对象保存到数据库中
36     */
37     public String add() throws Exception {
38         //通过uuid新建一个自动创建一个id
39         user.setId(UUID.randomUUID().toString());
40         service.addUser(user);
41         
42         return "addSUCCESS";
43 
44     }
45     /*
46      * 回显所有用户信息
47      */
48     public String list() throws Exception{
49         List<User> users =service.getAll();
50         
51         ServletActionContext.getRequest().setAttribute("users", users);
52         return "listSUCESS";
53     }
54     /*
55      * 删除某一个用户
56      */
57     public String del() throws Exception{
58         String id= ServletActionContext.getRequest().getParameter("id");
59         service.del(id);     //这里是删除掉这条用户
60         List<User> users =service.getAll();  //下面是为了把剩余的还显示在当前页面   
61         ServletActionContext.getRequest().setAttribute("users", users);
62         return "delSUCESS";
63     }
64     /*
65      * 更新用户
66      */
67     public String update() throws Exception{
68         //先通过id获得user对象
69         String id= ServletActionContext.getRequest().getParameter("id");
70         User user=service.find(id);
71         //再把对象转发到注册界面用于回显信息
72      ServletActionContext.getRequest().setAttribute("user", user);
73         //因为兴趣爱好也是写在java类中,所以需要在放到域中,用于回显
74      ServletActionContext.getRequest().setAttribute("preferences", Globals.preferences);
75      ServletActionContext.getRequest().setAttribute("types", Globals.types);
76         return "updateSUCESS";
77     }
78 }

 

posted on 2017-02-19 22:27  雨点的名字  阅读(266)  评论(0编辑  收藏  举报