(学习笔记)Struts2.3.3 入门与配置(一)

第一步:下载struts2.3.3  http://supergsego.com/apache/struts/2.3.30/struts-2.3.30-all.zip

第二步:新建一个WEB项目,导入以下9个jar架包

第三步:修改web.xml文件,配置struts2过滤器

<?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">
  
  <welcome-file-list>
    <welcome-file>index.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>

第四步:新建一个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>struts2</title>
</head>
<body>
	<form action="Login.action" method="post">
		username:<input type="text" name="username" /><br>
		password:<input type="password" name="password"/><br>
		<input type="submit" value="submit">
	</form>
</body>
</html>

第五步:在src目录新建一个包com.action,然后在包中新建一个类LoginAction

package com.action;

/**
 * index.jsp form表单处理
 * **/
public class LoginAction
{
	private String username;//必须和表单中的name属性一致
	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;
	}
	//这个方法必须要,处理返回最终结果,和配置文件result name="success" 对应
	public String execute()
	{
		return "success";
	}
}


第六步:在src目录中新建一个struts.xml文件,配置请求,action name="Login" 为 index.jsp 中 action的访问地址,后面跟.action

<?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>
	<!-- name随便填  必须继承struts-default namespace命名空间,说白了就是文件夹 -->
	<package name="struts2" extends="struts-default" namespace="/">
		<!-- action 一次请求, name为请求路径,这里是指index.jsp中form action的路径 -->
		<!-- class 为处理请求的具体实现类 -->
		<action name="Login" class="com.action.LoginAction">
		<!-- result 根据处理结果,转跳到相关页面 -->
			<result name="success">/result.jsp</result>
		</action>
	</package>
</struts>

第七步:新建一个result.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>result</title>
</head>
<body>
	${requestScope.username}<br>
	${requestScope.password}
</body>
</html>


最终结果如下:


最后,部署到tomcat 运行


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值