Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2313816
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Java

2011-12-21 16:47:09

声明: 在配置好Structs开发环境的基础上
 
1  配置Struts的映射地址
2  配置: 添加登录表单loginForm
3  配置:添加登录组件login
4  开发ActionForm Bean:创建登录表单类LoginForm.java
5  开发Action:创建登录处理类LoginAction.java
 
1  在使用Struts进行开发时 需要避免对JSP文件的直接访问 这样做的目的是为了将所有的请求都交给Struts来统一处理
在web.xml中进行映射配置
  1. <servlet-mapping>
  2.         <servlet-name>action</servlet-name>
  3.         <url-pattern>*.do</url-pattern>
  4.     </servlet-mapping>
对于每一个name.do的请求 都必须在struts-config.xml中配置与"name"对应的组件 该组件是通过元素来注册的 使用的第一种配置形式-直接转发方式 来配置一个地址"index.do" 负责转发到登录页面login.jsp
  1. <action path="/index" forward="/login.jsp" />
 
2  修改login.jsp的登录提交地址为login.do
  1. <form name="form1" action="login.do" method="post">
3  配置 : 添加登录表单loginForm
  1. <form-bean name="loginForm"
  2.             type="com.demo.struts.forms.LoginForm">
  3.         </form-bean>
4  配置:添加登录组件login
  1. <action path="/login" name="loginForm" scope="request"
  2.             type="com.demo.struts.actions.LoginAction" input="/login.jsp">
  3.             <forward name="success" path="/welcome.jsp" />
  4.             <forward name="failure" path="/login.jsp" />
  5.         </action>
5  开发ActionForm Bean:创建登录表单类 LoginForm.java
 
  1. package com.demo.struts.forms;

  2. public class LoginForm extends ActionForm {

  3.     proteced String username = nill;
         protected String password = null;

    public String getPassword() {

  1.         return password;
  2.     }

  3.     public void setPassword(String password) {
  4.         this.password = password;
  5.     }

  6.     public String getUsername() {
  7.         return username;
  8.     }

  9.     public void setUsername(String username) {
  10.         this.username = username;
  11.     }
  12. }

6  开发Action 创建登录处理类LoginAction.java

 

  1. package com.demo.struts.actions;

  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;

  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import javax.sql.DataSource;

  9. import org.apache.log4j.Logger;
  10. import org.apache.struts.action.ActionErrors;
  11. import org.apache.struts.action.ActionForm;
  12. import org.apache.struts.action.ActionForward;
  13. import org.apache.struts.action.ActionMapping;
  14. import org.apache.struts.action.ActionMessage;
  15. import org.apache.struts.actions.DispatchAction;


  16. public class LoginAction extends DispatchAction {


  17.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  18.             HttpServletRequest request, HttpServletResponse response)
  19.             throws Exception {

  20.         ActionErrors errors = new ActionErrors();
  21.         ActionForward forward = new ActionForward();
  22.         LoginForm loginForm = (LoginForm) form;

  23.         try {
  24.             // get parameters
  25.             String username = loginForm.getUsername();
  26.             String password = loginForm.getPassword();

  27.             // invalidate the original session if exists

  28.             HttpSession session = request.getSession(false);
  29.             if (session != null) {
  30.                 session.invalidate();
  31.             }

  32.             // create a new session for the user

  33.             session = request.getSession(true);

  34.             // login

  35.             boolean isValid = false;
  36.             if (username.equals("admin")&&password.equals("admin"))
  37.             {
  38.                isValid = true;
  39.              }
  40.             if (isValid) {
  41.                 session.setAttribute(Constants.USERNAME_KEY, username);
  42.             } else {
  43.                 errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
  44.                         "login.message.failed"));
  45.             }

  46.         } catch (Exception e) {
  47.             errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
  48.                     "login.message.failed"));

  49.         }

  50.         // If a message is required, save the specified key(s)

  51.         // into the request for use by the tag.


  52.         if (!errors.isEmpty()) {
  53.             saveErrors(request, errors);
  54.             
  55.             forward = mapping.findForward(Constants.FAILURE_KEY);
  56.         } else {
  57.             forward = mapping.findForward(Constants.SUCCESS_KEY);
  58.         }

  59.         // Finish with

  60.         return (forward);
  61.     }
  62.           
  63. }

 

 

 

阅读(1395) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~