Eclipse学习之三:Eclipse开发Struts应用
来源:互联网 发布:图片素材app软件排行 编辑:程序博客网 时间:2024/05/22 01:48
在配置好Structs开发环境的基础上
1 配置Struts的映射地址
2 配置<form-bean>: 添加登录表单loginForm
3 配置<action>:添加登录组件login
4 开发ActionForm Bean:创建登录表单类LoginForm.java
5 开发Action:创建登录处理类LoginAction.java
1 在使用Struts进行开发时 需要避免对JSP文件的直接访问 这样做的目的是为了将所有的请求都交给Struts来统一处理
在web.xml中进行映射配置
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
对于每一个name.do的请求 都必须在struts-config.xml中配置与"name"对应的组件 该组件是通过<action>元素来注册的 使用<action>的第一种配置形式-直接转发方式 来配置一个地址"index.do" 负责转发到登录页面login.jsp
- <action path="/index" forward="/login.jsp"/>
2 修改login.jsp的登录提交地址为login.do
- <formname="form1"action="login.do"method="post">
3 配置<form-bean> : 添加登录表单loginForm
- <form-bean name="loginForm"
- type="com.demo.struts.forms.LoginForm">
- </form-bean>
4 配置<action>:添加登录组件login
- <action path="/login" name="loginForm" scope="request"
- type="com.demo.struts.actions.LoginAction" input="/login.jsp">
- <forward name="success" path="/welcome.jsp"/>
- <forward name="failure" path="/login.jsp"/>
- </action>
5 开发ActionForm Bean:创建登录表单类 LoginForm.java
- package com.demo.struts.forms;
- public class LoginForm extends ActionForm {
- proteced String username = nill;
- protected String password = null;
public String getPassword(){
- return password;
- }
- public void setPassword(String password){
- this.password= password;
- }
- public String getUsername(){
- return username;
- }
- public void setUsername(String username){
- this.username= username;
- }
- }
6 开发Action 创建登录处理类LoginAction.java
- package com.demo.struts.actions;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import javax.sql.DataSource;
- import org.apache.log4j.Logger;
- import org.apache.struts.action.ActionErrors;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.ActionMessage;
- import org.apache.struts.actions.DispatchAction;
- public class LoginAction extends DispatchAction{
- public ActionForwardexecute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throwsException {
- ActionErrors errors = new ActionErrors();
- ActionForward forward = new ActionForward();
- LoginForm loginForm = (LoginForm) form;
- try {
- // get parameters
- String username= loginForm.getUsername();
- String password= loginForm.getPassword();
- // invalidate the original session if exists
- HttpSession session = request.getSession(false);
- if(session !=null) {
- session.invalidate();
- }
- // create a new session for the user
- session = request.getSession(true);
- // login
- booleanisValid = false;
- if (username.equals("admin")&&password.equals("admin"))
- {
- isValid = true;
- }
- if(isValid){
- session.setAttribute(Constants.USERNAME_KEY, username);
- }else {
- errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage(
- "login.message.failed"));
- }
- } catch (Exception e){
- errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage(
- "login.message.failed"));
- }
- // If a message is required, save the specified key(s)
- // into the request for use by the <struts:errors> tag.
- if (!errors.isEmpty()){
- saveErrors(request, errors);
- forward = mapping.findForward(Constants.FAILURE_KEY);
- } else {
- forward = mapping.findForward(Constants.SUCCESS_KEY);
- }
- // Finish with
- return(forward);
- }
- }
- Eclipse学习之三:Eclipse开发Struts应用
- eclipse搭配Resin开发Struts应用之环境篇
- eclipse搭配Resin开发Struts应用之环境篇
- eclipse搭配Resin开发Struts应用之环境篇
- Eclipse学习之二:Eclipse开发JavaWeb应用
- 配置eclipse 3.4开发struts应用
- 配置eclipse 3.4开发struts应用
- 开发工具之Eclipse(三)
- eclipse开发RCP之三
- 用Eclipse开发struts之完美方案
- Eclipse开发struts
- Eclipse开发struts指南
- Eclipse开发struts
- 集成Eclipse和InforWeb开发第一个Struts应用
- 集成Eclipse和InforWeb开发第一个Struts应用
- 集成Eclipse和InforWeb开发第一个Struts应用
- 使用eclipse+lomboz开发struts+hibernate+spring应用
- Eclipse插件,struts的应用
- 深入浅出JSON
- 运动启发智慧
- asp.net三层架构详解
- android Sim卡锁定 pin解锁流程学习
- SWT、RCP编程常用网站
- Eclipse学习之三:Eclipse开发Struts应用
- C++函数重定义、重载、重写 详细出处参考:http://www.ityoudao.com/Web/VC_588_828.html
- 4~20mA电流环采集电路
- position:relative 的深入理解,欢迎探讨
- C++中智能指针的设计和使用 .
- sipXecs编码标准
- 为什么一般数据恢复软件恢复的MDF文件无法附加
- 让Notepad++调用GCC直接编译源代码
- jsp