struts2基于注解的配置Demo

这篇博客提供了一个基于注解的Struts2 Demo,适用于学习者。博主应同学需求,创建了一个可运行的项目,包含项目依赖、web.xml配置、struts.xml配置和项目结构说明,适用于JDK1.7、Tomcat7和Eclipse环境。
摘要由CSDN通过智能技术生成

应同学要求,给找个Struts2基于注解的小例子,本以为网上有好多,结果她说不好用,我自己毕业后就没用过Struts了,试着在网上找了一些,果然项目都不能很好的运行,于是决心自己写个小Demo,也算给后来学习者一个可运行的Demo吧!

项目下载地址:

点击下载

一、项目依赖的包,我一块打包到项目中了,各位可以先下载项目

二、既然是Struts2的,那么肯定少不了web.xml里面配置一下Struts2的相关信息

下面开始讲述整个项目配置的例子

项目运行环境jdk1.7+tomcat7+eclipse

项目结构截图



1.首先配置web.xml

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<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>
	
	<display-name>Struts2Demo</display-name>
	<welcome-file-list>
		<welcome-file>/WEB-INF/index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


2.配置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.0.dtd">

<struts>


	<!-- 请求参数的编码方式 -->
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->
	<constant name="struts.action.extension" value="action,do,go,htm" />
	<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
	<constant name="struts.configuration.xml.reload" value="true" />
	<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->
	<constant name="struts.devMode" value="false" />
	<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->
	<constant name="struts.serve.static.browserCache" value="false" />
	<!-- 指定由spring负责action对象的创建 -->
	<!-- <constant name="struts.objectFactory" value="spring" /> -->
	<!-- 是否开启动态方法调用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />

	<!-- 这一点也是,好多网上教程,居然是配置的default -->
	<constant name="struts.convention.default.parent.package" value="convention-default" />
	
	<!-- 这一点网上好多教程都没有,既然是注解,这个扫描包的都不加,搞毛线啊 -->
	<constant name="struts.convention.action.packages" value="com.lkx" />

</struts> 

3.Action类

package com.lkx;

import com.opensymphony.xwork2.ActionSupport;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

/***
 * Struts2 基于注解配置的Action
 * 
 * @author 素剑步青尘
 * 
 */

public class HelloWorld extends ActionSupport {

	private String username;
	private String password;
	private String msg;

	@Action(value="/welcome",results={
	        @Result(name = "success", location = "/WEB-INF/result.jsp"),
	        @Result(name = "error", location = "/WEB-INF/result.jsp")}
	)
	public String execute() {
		if (username.equals("lkx") && password.equals("lkx")) {
			setMsg("恭喜你,登录成功!!!");
			return SUCCESS;
		} else {
			setMsg("用户名或密码错误");
			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;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

}

4.接下来就是两个jsp页面就算是搞定了

index.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK"%>
<!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=GBK">
<title>Hello world</title>
</head>
<body>
	<form action="/Struts2Demo/welcome.action" method="post">
		用户名:<input type="text" name="username" value="lkx" /> 
		密 码:<input  type="text" name="password" value="lkx" /> 
		<input type="submit" value="提交" />
	</form>
</body>
</html>

result.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s"%>     
<!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=GBK">
<title>登录成功--success!!!</title>
</head>
<body>
${username} ${msg}
</body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值