1.在tomcat的webapp中创建一个WEB工程myapp,具体的文件及路径如下所示:

①/webapp/myapp/login.jsp

②/webapp/myapp/welcom.jsp

③/webapp/myapp/error.jsp

④/webapp/myapp/WEB-INF/web.xml

⑤/webapp/myapp/WEB-INF/classes

⑥/webapp/myapp/WEB-INF/classes/struts.xml

⑦/webapp/myapp/WEB-INF/classes/test/LoginAction.class

⑧/webapp/myapp/WEB-INF/lib

 

2.在lib中添加如下jar文件:commons-fileupload-1.3.1、commons-io-2.2、commons-lang3-3.2、commons-logging-1.1.3、freemarker-2.3.19、javassist-3.11.0.GA、ognl-3.0.6、struts2-core-2.3.20、xwork-core-2.3.20(具体版本根据自己使用的Struts2的版本做调整)

 

3.源代码

#login.jsp

---------------------------------------------------------------------------------

<%@page language="java" contentType="text/html;charset=UTF-8"%>


</span><span style="font-size:14px;font-family:'微软雅黑', 'sans-serif';">登陆页面



  

登陆页面


  
   username
  
  
   password
  
  
   
  
 


 

#welcome.jsp

---------------------------------------------------------------------------------------

<%@page language="java" contentType="text/html;charset=UTF-8"%>


</span><span style="font-size:14px;font-family:'微软雅黑', 'sans-serif';">成功页面


您已经登陆!

 

#error.jsp

--------------------------------------------------------------------------------

<%@page language="java" contentType="text/html;charset=UTF-8"%>


</span><span style="font-size:14px;font-family:'微软雅黑', 'sans-serif';">失败页面


登陆失败!

 

#LoginAction.java

-----------------------------------------------------------------------

packagetest;
public class LoginAction
{
 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
 {
  if (getUsername().equals("scott") &&getPassword().equals("tiger") )
  {
   return "success";
  }
  else
  {
   return "error";
  }
 }
}

 

4.web.xml配置

--------------------------------------------------------------------------------------


http://java.sun.com/xml/ns/j2ee
"
         xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   
       struts2
       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   

   
       struts2
       /*
   

5.Struts2.xml配置

--------------------------------------------------------------------------------------


 "-//Apache Software Foundation//DTD Struts Configuration2.3//EN"
 "
http://struts.apache.org/dtds/struts-2.3.dtd">


   
   
   
  
            /welcome.jsp
            /error.jsp
       

   

 

6.常见问题

1)输入http://localhost:8080/myapp/login.jsp提示:HTTPStatus 404 - /myapp/login.jsp

主要原因可能有:①lib中的jar文件不够

②struts.xml中的package与action配置有误

2)login.jsp页面点击submit提交后提示空指针错误

主要原因可能有:①LoginAction没有包名,直接存放在了classes的根目录下