完整的struts2框架应用实例

       一、有关struts的配置,上一篇文章已经介绍过了,这里不再赘述,直接进入主题;

       配置网址:(http://blog.csdn.net/linshenshijianlu/article/details/78050209);

      二、struts框架所需要的两个配置文件:web.xml和struts.xml

    web.xml配置文件主要是配置struts的过滤器,使整个web的流程转入到struts框架中,而struts.xml是struts框架的核心配置文件,在项目开发过程中,需要在此文件中进行大量的配置;

     三、了解之后,开始进入实例开发 

        3.1  建立一个项目,名称为FirstAction

       3.2  导入框架所需要的各种jar包(在WEB-INF/lib下,注意一定要确保位置的正确性,否则会因为找不到路径而报错);

       3.3  配置web.xml




  login.jsp


struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


struts2
/*

     3.4 在src下建立一个com.action包,并在包下建立一个java类,注意引用的superclass是com.opensymphony.xwork2.ActionContext;

     

package com.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
  private String username;
  private String password;
public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
  public String execute() throws Exception{
	  String strReturn=INPUT;
	  if(this.username.equals("abc")&&this.password.equals("123")){
		  
		  strReturn=SUCCESS;
	  }else{
		  
		  ActionContext.getContext().getSession().put("tip","登录失败");
	  }
	  return strReturn;
  }		
}
     3.5 再建立两个JSP页面,分别是login.jsp和loginSuccess.jsp(注意这两个页面的放置位置,如果放错,则会出不来结果)

    login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登录界面


 
${tip}
用户名:
密码:
loginSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登录成功


  用户登录成功



   3.6  struts.xml配置文件

  (红色内容的引用,参考上面的网址)




   
        
            loginSuccess.jsp
            login.jsp
        
    

   3.7结果显示:

  完整的struts2框架应用实例_第1张图片
   
    目录结构:(注意:一定要确保每一个文件位置的正确性,不然,很容易出错的;)

   (lib下的jar文件,使用复制的方式;)

  完整的struts2框架应用实例_第2张图片



     

       

      

你可能感兴趣的:(struts2框架)