完整的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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4">
<welcome-file-list>
  <welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
     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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
</head>
<body>
 <center>
    ${tip}
    <form action="login.action" method="post">
      <table>
        <tr>
          <td>用户名:</td>
          <td><input type="text" name="username"/></td>
        </tr>
        <tr>
          <td>密码:</td>
          <td><input type="text" name="password"/></td>
        </tr>
        <tr>
         <td colspan="2"><input type="submit" value="登录"/></td>
        </tr>
      </table>
    </form>
 </center>
</body>
</html>
loginSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
  用户登录成功
</body>
</html>

   3.6  struts.xml配置文件

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd" >
<struts>
   <package name="myPackage" namespace="/" extends="struts-default">
        <action name="login" class="com.action.UserAction" method="execute">
            <result>loginSuccess.jsp</result>
            <result name="input">login.jsp</result>
        </action>
    </package>
</struts>
   3.7结果显示:

  
   
    目录结构:(注意:一定要确保每一个文件位置的正确性,不然,很容易出错的;)

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

  



     

       

      

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值