J2EE dynamic web工程搭建 struts2

本文介绍了一个使用Struts2框架实现的简单登录示例,包括必要的配置文件(struts.xml和web.xml)设置及Java Action类(LoginAction)的编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工程目录:


搭建struts2关键的jar一共有9个,分别是:


LoginAction.java 类:

package com.cy.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	
	private static final long serialVersionUID = 1L;
	 
	    private String userName;
	    private String password;
	    @Override
	    public String execute() {
 
	         if (userName.equals("hellokitty") && password.equals("123")) {
	
	            return SUCCESS;
	        } else {
	             return ERROR;
	         }
	 
	     }
	
	    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;
	    }
	
}
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/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsDemo</display-name>
  <welcome-file-list>
     <welcome-file>index.html</welcome-file>
     <welcome-file>index.htm</welcome-file>
     <welcome-file>index.jsp</welcome-file>
     <welcome-file>default.html</welcome-file>
     <welcome-file>default.htm</welcome-file>
     <welcome-file>default.jsp</welcome-file>
     </welcome-file-list>
     <!-- struts2.1.3之后的版本,可以在该过滤器之前之间定义一定的过滤器-->
     <!-- 定义struts2 的核心控制器,用于生成ActionMapper ,拦截所有的Action请求-->
    <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>
struts.xml  这个xml一定要放置在src目录下面:

<?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
      "http://struts.apache.org/dtds/struts-2.3.dtd">
   <struts>
      <package name="default" namespace="/" extends="struts-default">
          <action name="login" class="com.cy.action.LoginAction">
              <result name="success">/jsp/success.jsp</result> 
              <result name="error">/jsp/error.jsp</result>      
         </action>
     </package>

 </struts>
login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
  String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 %>
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
   <head>
     <base href="<%=basePath%>">
     
     <title>My JSP 'Login.jsp' starting page</title>
     
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="cache-control" content="no-cache">
     <meta http-equiv="expires" content="0">    
     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     <meta http-equiv="description" content="This is my page">
    <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->
   </head>
      <body>
     <form action="login.action" method="get">
               用户名:<input type="text" name="userName"><br/>
               密    码:<input type="password" name="password"/><br/>
             <input type="submit" value="提交"/> 
           <input type="reset" value="重置"/>                  
    </form>
   </body>
</html>

success.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <h1>success!!!!!!!!!</h1>
</body>
</html>

error.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>error!!!!!!!!!</h1>
</body>
</html>

默认页面index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  struts2.
</body>
</html>

在调试的过程中,出现的错误:

type Status report
message There is no Action mapped for namespace [/] and action name [login] associated with context path [/structDemo].
description The requested resource is not available.


1.表单提交的login.action 不是login

 <form action="login.action" method="get">
               用户名:<input type="text" name="userName"><br/>
               密    码:<input type="password" name="password"/><br/>
             <input type="submit" value="提交"/> 
           <input type="reset" value="重置"/>                  
    </form>

2.namespace需要添加

<package name="default" namespace="/" extends="struts-default">

3.Java action类 execute方法外面添加 @Override 注解

  @Override
	    public String execute() {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值