Struts2事实上并不是Struts 1的升级版,而是WebWork的升级版。因为Struts2是WebWork的升级,而不是一个全新的框架,因此稳定性、性能等各方面都有很好的保证:而且吸收了Struts 1和WebWork两者的优势,是一个好的框架,使用起来,非常的简洁,干净,强大。下面我们就用Struts2来创建一个简单的登录实例。
1、首先当然是在WEB工程里建立好Struts2框架,在这里过程中,我们需要到apache官网下载Struts2的包,我这里下好的是struts-2.1.8.1-all.zip,下好后,解压出来,把所需的commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar、freemarker-2.3.15.jar、ognl-2.7.3.jar、struts2-core-2.1.8.1.jar、struts2-pell-multipart-plugin-2.1.8.1.jar、xwork-core-2.1.6.jar这几个包拷到项目的lib下,再在web.xml中加入Struts2的过滤器,如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app 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/j2ee
- http://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>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
2、接下来我们可以开始工作了,首先写一个JSP页面
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <html>
- <head>
- <title> 登录页面 </title>
- <link rel=stylesheet href="css/login.css" type="text/css">
- </head>
- <body>
- <h1 align="center">用户登录页面</h1>
- <hr>
- <div align="center">
- <form action="login.lp" method="post">
- <table cellspacing=5 border=5 bodercolor=#ffaa00 >
- <tr><th colspan="3" align="center" bgcolor=#ffaa00>用户登录</th></tr>
- <tr>
- <th rowspan="3" background="images/2.jpg" style="width=90px"></th>
- <td>用户名:</td><td><input type="text" class="message" name="username"></td></tr>
- <tr><td>密 码:</td><td><input class="message" type="password" name="userpassword"></td></tr>
- <tr><td colspan="2" align="center"><input type="submit" value="登录"> <input type="reset" value="重置"></td></tr>
- </table>
- </form>
- </div>
- </body>
- </html>
3、写好JSP页面,接下来要写Struts2的action,它的action比Struts1的要简洁的多,非常的方便。如下:
- package myclass.struts2.action;
- public class LoginAction {
- private String username;
- private String userpassword;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getUserpassword() {
- return userpassword;
- }
- public void setUserpassword(String userpassword) {
- this.userpassword = userpassword;
- }
- public String execute(){
- System.out.println("我进来了呢");
- System.out.println(this.getUsername());
- System.out.println(this.getUserpassword());
- if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){
- return "succ";
- }else{
- return "fail";
- }
- }
- }
同时也写个退出系统的action吧,
- package myclass.struts2.action;
- public class LogoutAction {
- public String execute(){
- System.out.println("退出系统");
- return "exit";
- }
- }
写好后,就可以来配置struts.xml配置文件了,这是同样与struts.1一样是核心,struts.2可以支持分开来管理不同的xml文件,所以我们把登入的action与登出的action分别配置一下,如下:
登入的:
- <?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>
- <!--使用扩展名常量,使请求的扩展名为lp,可以根据你的需要设置成任何格式-->
- <constant name="struts.action.extension" value="lp" />
- <package name="login" namespace="/" extends="struts-default">
- <action name="login" class="myclass.struts2.action.LoginAction">
- <result name="succ">/succ.jsp</result>
- <result name="fail">/failure.jsp</result>
- </action>
- </package>
- </struts>
登出的:
- <?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>
- <constant name="struts.action.extension" value="lp" />
- <package name="logout" namespace="/" extends="struts-default">
- <action name="logout" class="myclass.struts2.action.LogoutAction">
- <result name="exit">/exit.jsp</result>
- </action>
- </package>
- </struts>
result节点配置的是action跳转的页面,通过与name的值匹配。然后把这两个xml放到struts中来:
- <?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>
- <include file="login.xml"></include>
- <include file="logout.xml"></include>
- </struts>
这样,登录的实例就做好了,接下来开启tomcat运行即可!