struts2入门基本配置

1、下载struts2的包

2、新建一个项目,导入如下包

以后根据需要再添加其他的包,有了上面的包,项目已经可以使用struts2了

3、打开项目的web.xml文件,添加struts2的过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
		<!-- 
		<init-param>
			<param-name>actionPackages</param-name>
			<param-value>com.mycompany.myapp.actions</param-value>
		</init-param> -->
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

4、新建一个login.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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="<%=path%>/userAction.action" method="post">
    username:<input type="text" name="username"><br>
    password:<input type="text" name="password"><br>
    <input type="submit" value="提交">
   </form>
  </body>
</html>

5、新建一个action类,action类有三种实现方式,分别是普通的javabean,里面包含无参构造函数,和execute方法,第二种是继承了ActionSupport类,第三种是实现Action接口。通常使用第二种方式。

package com.struts2.action;

public class PojoAction {
	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(){
		System.out.println("username="+username+":::password="+password);
		return "index";
	}
}


6、注意其中有username和password两个属性和get/set方法。当页面请求这个action时,struts2会自动把jsp页面上name属性的值赋值给对应的java类里的属性。

7、struts2.xml放在src目录下

<?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.0.dtd">

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="useraction" namespace="/" extends="struts-default">
        <action name="userAction"  class="com.struts2.action.PojoAction">
            <result name="index">index.jsp</result>
        </action>
    </package>

    <!-- Add packages here -->

</struts>

8、当请求道userAction时,会执行execute方法,根据返回值index,找到对应的结果集配置的页面。

9、访问http://localhost:8080/struts2Study/login.jsp,跳转到index.jsp页面


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值