struts2初学实现用户显示

刚刚开始学习struts2,就从最简单的增删查改开始做吧。

今天就先完成显示功能的是实现吧

首先在开始做之前我们需要对struts2项目进行配置

其中mysql-connector-java-5.1.45是数据库jar包,jstl和standard是el表达式使用的包,其余就是struts最基本的jar包

完成jar包之后便是web.xml和struts2.xml的配置了

首先web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app id="starter" version="2.4"
 3          xmlns="http://java.sun.com/xml/ns/j2ee"
 4          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 6   
 7     <display-name>struts2</display-name>
 8     <display-name>Struts 2 Rest Example</display-name>
 9   <!-- Filters -->
10   <!-- START SNIPPET: filter -->
11     <filter>
12         <filter-name>action2</filter-name>
13         <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
14     </filter>
15     <!-- END SNIPPET: filter -->
16 
17     <filter-mapping>
18         <filter-name>action2</filter-name>
19         <url-pattern>/*</url-pattern>
20     </filter-mapping>
21 
22     <!-- Welcome file lists -->
23     <welcome-file-list>
24         <welcome-file>index.jsp</welcome-file>
25     </welcome-file-list>
26     
27 </web-app>

struts2.xml

1 <?xml version="1.0" encoding="UTF-8" ?>
2 
3 <!DOCTYPE struts PUBLIC
4         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
5         "http://struts.apache.org/dtds/struts-2.5.dtd">
6 <struts>
7 
8 </struts>

这里我们暂时还没有写功能所以什么都没有,如果是在官方测试包中提取的struts2。xml中间有一些东西,需要手动删除

在完成了这些之后运行服务器看是否报错,若无错误便是struts2配置完成

第一步创建 用户类

 1 package model;
 2 
 3 public class UserModel {
 4     
 5     private int id;
 6     private String userName;
 7     private String pwd;
 8     
 9     public UserModel(){}
10 
11     public UserModel(int id, String userName, String pwd) {
12         super();
13         this.id = id;
14         this.userName = userName;
15         this.pwd = pwd;
16     }
17     public int getId() {
18         return id;
19     }
20     public void setId(int id) {
21         this.id = id;
22     }
23     public String getUserName() {
24         return userName;
25     }
26     public void setUserName(String userName) {
27         this.userName = userName;
28     }
29     public String getPwd() {
30         return pwd;
31     }
32     public void setPwd(String pwd) {
33         this.pwd = pwd;
34     }
35 
36 }
UserModel

第二步创建 Dao类

 1 package dao;
 2 
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 
 8 import javax.security.auth.message.callback.PrivateKeyCallback.Request;
 9 
10 import model.UserModel;
11 import utils.JDBCUtil;
12 
13 public class UserDao {
14     private String sql = "";
15     
16     public List<UserModel> selectUser(){
17         sql="select * from user";
18         boolean flag = false;
19         ResultSet rs = JDBCUtil.doQuery(sql);
20         List<UserModel> uList = new ArrayList<UserModel>();
21         try {
22             while(rs.next()){
23                 UserModel uModel = new UserModel(
24                         Integer.parseInt(rs.getString("id")),
25                         rs.getString("userName"),
26                         rs.getString("pwd"));
27                 uList.add(uModel);
28                 
29             }
30         } catch (SQLException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }
34         return uList;
35     }
36 
37 }
UserDao

第三步编写Action

 1 package action;
 2 
 3 import java.util.List;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 import dao.UserDao;
10 import model.UserModel;
11 
12 public class DisplayAction {
13     private UserModel user;
14 
15     public UserModel getUser() {
16         return user;
17     }
18 
19     public void setUser(UserModel user) {
20         this.user = user;
21     }
22     public String execute(){
23         UserDao dao = new UserDao();
24         List<UserModel> uList = dao.selectUser();
25         HttpServletRequest request = ServletActionContext.getRequest();   
26         request.setAttribute("userList", uList);  
27         return "success";
28     }
29 }
displayAction

在这里我们将数据库取出的数据全部存入request中方便jsp页面使用el表达式来提取

呢么完成这些后struts2的后台已经完成了,最重要的一步配置struts2.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 5         "http://struts.apache.org/dtds/struts-2.5.dtd">
 6 <struts>
 7     
 8     <package name="user" namespace="/" extends="struts-default">
 9         <action name="display" class="action.DisplayAction" method="execute">
10             <result name="success" type="dispatcher">/display.jsp</result>
11         </action>
12     </package>
13 </struts>

这里其中namespace的作用便是 如果你写的是 / a 呢么在你访问的页面路径之前都会有/a 如果只写 / 呢么无影响 extends 这里内容固定不变。

下面action 中 name 表示访问的名称, class表示访问的直至 ,method 表示访问的方法

result 表示重定向

最后两个jsp的页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>功能模板页面</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18   </head>
19   
20   <body>
21       <a href="display">显示</a>
22   </body>
23 </html>
主页面
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'display.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19 
20 
21   </head>
22   
23   <body>
24       <table>
25         <tr><th>id</th><th>昵称</th><th>密码</th></tr>
26         <c:forEach items="${userList}" var="user">
27             <tr><td>${user.id }</td><td>${user.userName }</td><td>${user.pwd }</td></tr>
28           </c:forEach>
29       </table>
30   </body>
31 </html>
显示页面

 

搞定

 

posted @ 2018-02-19 00:30  SmallDemons  阅读(427)  评论(0)    收藏  举报