struts2配置

1.下载所需包 在struts2 官网下载相关包,插入到WebConetent/WEB-INF/lib下,struts2里的包比较多,可以先添加一些基本的包,再根据调试结果添加。我程序添加如下包即可运行:

2.新建一个项目,按1添加包。

3.配置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">
  <display-name>FirstStruts2</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 过滤器!  -->
  <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>
4.配置struts.xml(不是struts2.xml) 这里说下项目的功能,就是index.jsp中有个表单,如果输入为空则转到error.jsp,否则转到success.jsp

<?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="cqu" extends="struts-default">
    	<!-- 定义action -->
    	<action name="greeting" class="cqu.GreetingAction">
    		<!-- 定义成功的映射页面 -->
    		<result name="success">success.jsp</result>
    		<!-- 定义失败的映射页面 -->
    		<result name="error">error.jsp</result>
    	</action>
    </package>
</struts>
注意上面的DOCTYPE声明中的dtds版本(我的版本是struts-2.3.16.3)

5.GreetingAction用来处理表单信息,判断是不是无效的

package cqu;

import com.opensymphony.xwork2.ActionSupport;

public class GreetingAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String username;
	public String execute() throws Exception{
		if(username==null||"".equals(username)){
			return ERROR;
		}else {
			return SUCCESS;
		}
	}
	public String getUsername(){
		return username;
	}
	public void setUsername(String username){
		this.username=username;
	}
}
6.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>
	<form action="greeting.action" method="post">
	please input your name:<input type="text" name="username">
	<input type="submit" value="submit">
	</form>
<body>

</body>
</html>
7.success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<s:property value="username"/><br>
welecome@!!!!!!!!
</body>
</html>

其中
<s:property value="username"/>
是获取提交表单中的数据,<s:/>是struts2的标签。使用之间要插入该标签库,见第三行。

8.errror.jsp略

9.下面是整个项目目录,注意xml存放的位置!!!!jsp文件放在WebContent下web.xml放在WEB-INF下,struts.xml放在src下而不是放在包内。





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值