struct2环境搭建与例子

环境搭建

struct2需要去网上下载所需要的包。自行下载吧。下载好了随便新建一个工程。然后导入刚才下载的包。

例子解析

首先来看看我的工程
我们先来配置一下web.xml这个文件
<?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">
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struct2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struct2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>
主要是配置过滤器和它的映射,然后在src的源码目录下面新建一个structs.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>
    <package name="default" namespace="/" extends="struts-default">
        <action name="login" class="com.action.LoginAction" method="execute">
            <result name="OK">/welcom.jsp</result>
            <result name="ERROR">/login.jsp</result>
            <result name="input">/login.jsp</result>
        </action>
    </package>
  <constant name="struts.custom.i18n.resources" value="messageResource"></constant>
</struts>
这里主要配置的是执行的ACTION注意后面的constant这个是为了实现国际化才加的依托的是另外一个文件messageResource_zh_CN.properties这个文件,看看这个文件里面有什么东西
login.title=\u767B\u5F55
login.passwd=\u5BC6\u7801
erro=\u9519\u8BEF
就是这几行主要就是登录,密码 ,错误这几个字。因为编码方式所以看不出来。然后就是看看我们的ACTION是怎么处理的。
**
 * 定义的Action
 * 
 * 
 */
public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String username;
	private String password;

	public LoginAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	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 void validate() {
		// TODO Auto-generated method stub
		if (getUsername() == null||getUsername().equals("")) {
			this.addFieldError("username", getText("erro"));
		}
		if (getPassword() == null) {
			this.addFieldError("password", "密码错误");
		}
		super.validate();
	}

	/**
	 * 结果返回
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if (getUsername().equals("admin") && getPassword().equals("123456"))
			return "OK";
		else
			return "ERROR";
	}

}
最重要的应该是execute方法这个方法返回的结果与structs.xml里面配置相对应这样就可以跳转不同的页面了。
welcom.jsp里面的代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'welcome.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">
 

  </head>
  
  <body>
   欢迎${username }!
   <br>
   ${password}
  </body>
</html>

login.jsp里面的代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>Login</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">

  </head>
  <body>
   
   <s:form action="/login" method="post">
    <s:label value="系统登陆">  </s:label>
    <s:textfield name="username" key="login.title"  />
    <s:password name="password" label="密码" />
    <s:submit value="登录" />
   </s:form>
  
  </body>
</html>

好了如果没有问题就可以在tomcat里面跑起来了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值