码迷,mamicode.com
首页 > 其他好文 > 详细

不用IDE,进行struts2开发的简单实例

时间:2015-04-04 01:27:03      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:struts2

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"%>
<html>
<head>
<title>
登陆页面</title>
</head>
<body>
<form action="Login.action" method="post">

<tablealign="center">
  <caption><h3>
登陆页面</h3></caption>
  <tr>
   <td>username<input type="text"name="username"/></td>
  </tr>
  <tr>
   <td>password<input type="text"name="password"/></td>
  </tr>
  <tr>
   <td colspan="2"><inputtype="submit" value="submit"/><inputtype="reset" value="reset" /></td>
  </tr>
 </table>
</form>
</body>
</html>

 

#welcome.jsp

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

<%@page language="java" contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>
成功页面</title>
</head>
<body>
您已经登陆!
</body>
</html>

 

#error.jsp

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

<%@page language="java" contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>
失败页面</title>
</head>
<body>
登陆失败!
</body>
</html>

 

#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配置

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

<?xmlversion="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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/j2eehttp://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>
</web-app>

5.Struts2.xml配置

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

<?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration2.3//EN"
 "
http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constantname="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode"value="true" />
    <package name="default" namespace="/"extends="struts-default">
  <action name="Login"class="test.LoginAction" method="execute">
            <resultname="success">/welcome.jsp</result>
            <resultname="error">/error.jsp</result>
        </action>
    </package>
</struts>

 

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的根目录下

本文出自 “7121961” 博客,谢绝转载!

不用IDE,进行struts2开发的简单实例

标签:struts2

原文地址:http://7131961.blog.51cto.com/7121961/1628103

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!