使用myeclipse配置struts2以及简单测试

首先先创建一个web工程,方法是 File->new->web project
在弹出的窗口中填写你要创建的web工程名字 比如mystruts2
然后编辑web.xml文件,将下面代码复制进去

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>mystruts2</display-name>
  <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>

然后加入struts2的一些包,方法是在工程上面右键,然后myeclipse->project factes->install apache struts(2.x)Factes
编辑生成的struts.xml将下面代码复制进去

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>


	<package name="login"  extends="struts-default">
		<action name="login" class="com.login.struts2.login">
		<result name="input">/login.jsp</result>
			<result name="success">/success.jsp</result>
			<result name = "failer">/failer.jsp</result>
		</action>
	</package>


</struts>    

然后创建com.login.struts2包,在里面新建login.java.然后加入下面代码

package com.login.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class login {
	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() throws Exception{
		
		System.out.println(this.getUsername());
		System.out.println(this.getPassword());
		if(getUsername().equals("test")&&getPassword().equals("test"))
		{
			System.out.println("this is ok");
			return "success";
		}
		else
		{
			
			System.out.print("failer");
			return "failer";
		}
			
	}
}

login.jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
	<head><title>Login page</title></head>
	<body>
		<b>Login page - Login to the Struts 2 application</b>
		<hr />
		<s:form  action = "login">
		<s:textfield name = "username" label = "username"></s:textfield>
		<s:password name = "password" label = "password"></s:password>
		<s:submit></s:submit>
		</s:form>
	</body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>success.jsp</title>
<body>
    success. <br>
  </body>
</html>

failer.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
<title>>failer.jsp</title>
  </head>
  
  <body>
    failer. <br>
  </body>
</html>

然后就可以在浏览器里面测试了

发表评论

电子邮件地址不会被公开。 必填项已用*标注