如果看不到WEB-INF文件夾之下的classes文件夾,可以利用如下方法解決該問題:
打開Navigator視圖就可以看到(在bin文件夾下),打開改視圖的方法:Window→Show View→Navigator。
然後在classes文件夾下面新建一個struts.xml文件。結果如下:
下面對整個示例程序做一個介紹,實現的功能主要是:
下面分別給出各個文件的詳細內容:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>first struts 2 project</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>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="struts2_login" extends="struts-default" namespace="/login">
<action name="checkLogin" class="act.LoginAction" method="checkLogin">
<result name="success">/index.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
</struts>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html><body>登錄成功,歡迎您!</body></html>
login.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<style type="text/css">
*{font-size:12px;}
</style>
<html>
<body>
<div style="margin:30px 50px 20px 50px; text-align:center">
<div>
<s:form action="checkLogin" namespace="/login">
<s:textfield name="username" label="登錄名稱"/>
<s:password name="password" label="登錄密碼"/>
<s:submit value="登錄"/>
</s:form>
</div>
</div>
</body>
</html>
LoginAction.java
package act;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
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 checkLogin()
{
if(this.username.equals("davenzhang")&&this.password.equals("123456"))
return SUCCESS;
else
return LOGIN;
}
}
登錄界面如下
加入的lib包有以下一些:
不然會報錯!