Struts2 入门训练1

本文介绍了一个基于Struts2框架实现的简单登录验证系统的搭建过程。从项目配置到核心代码编写,包括所需JAR包的引入、JSP页面的设计、Action类的实现及Struts配置等关键步骤。

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

之前有学了一点struts,不过才开始认真写代码,很基础。


首先是WebContent/WEB-INF/lib导入一些基本包

     asm-3.3.jar

    asm-commom-3.3.jar

    asm-tree-3.3.jar

   commons-fileupload-1.3.1.jar

   commons-io-2.2.jar

   commons-lang3-3.2.jar

   freemarker-2.3.22.jar

   javassist-3.11.0.GA.jar

   ognl-3.0.6.jar

  struts2-core-2.3.24.jar

  xwork-core-2.3.24.jar

 

然后WebContent下创建三个文件

  error.jsp

<%@ page language="java" 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>Insert title here</title>
</head>
<body>
   <p>${error}<br></p>
</body>
</html>
 

 success.jsp

<%@ page language="java" 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>Insert title here</title>
</head>
<body>
  <p>${success}<br></p>
  <h2>用户登录信息</h2>
     用户名:${username}<br>
   密码:${password}<br>
</body>
</html>


login.jsp

<%@ page language="java" 
    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>Insert title here</title>
</head>
<body>
 <div align="center">
    <form action="login" method="post">
                  用户名:<input type="text" name="username"/><br>
                 密码:<input type="password" name="password"/><br>
      <input type="submit" value="登录™†"/>
    </form>
 </div>
</body>
</html>

然后WEB-INF/下创建一个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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Two</display-name>
  <welcome-file-list>
    <welcome-file>index.html</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>


在src目录下创建一个包,cn.itcast.action

  创建一个java文件

Login.Action.java

package cn.itcast.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction 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; 	 
     }
     @Override
     public String execute() throws Exception{
    	ActionContext context = ActionContext.getContext();
    	if("itcast".equals(username) && "123".equals(password)){
    		context.put("username", username);
    		context.put("password", password);
    		context.put("success", "用户登录成功");
    		return SUCCESS;
    	}
    	else{
    		context.put("error", "用户登录失败");
    		return ERROR;
    	}
     }
}

在src下创建一个struts.xml文件

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


到这里一个简单的登录验证框就出来了。

期间碰到了一些问题。

1  要点击server 里面的tomcat项目进去看到一个server option选择第二个框勾上,不然会出现错误,

Publish module contexts to separate XML files 这一项记得勾上

2  server Location里面一开始是勾上第一项的,有可能要选择第二项才可以,

 Use Tomcat installation这一项

3 还有就是一些包名打错检查了好一会儿。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值