Struts2简单实例

来源:互联网 发布:linux system返回值 编辑:程序博客网 时间:2024/05/17 10:56

 本文使用Struts2演示一个最最简单的登录的实例,旨在初步了解Struts2.

首先,看一下实例的目录结构


需要引入的JAR包如下图


web.xml的内容:

<?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"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><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>
注:与Struts1不同的是,Struts2不再需要使用servlet标签来配置ActionServlet,取而代之的是用filter来配置。当然,核心处理类也不同了。


Strut.xml配置文件的内容

<?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><package name="test2" namespace="/user" extends="struts-default"><action name="Login" class="com.action.LoginAction2" method="aaa"><result name="success">/Success.jsp</result><result name="error" type="redirect">/Error.jsp</result></action></package></struts>
注:Strut.xml与struts1中的struts-config.xml文件对应,但此文件的配置信息比struts-config.xml的配置信息简单了很多,不再需要配置actionForm,因为在struts2的世界里已经没有actionform这一概念啦。


User.java的内容

package com.entity;public class User {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;}}
注:如果非要跟struts1中的某个文件对应一下,我感觉应该是struts1中的actionform吧,但与之不同的是,这里的user类是pojo对象,不需要继承任何类。


LoginAction2.java中的内容

package com.action;import com.entity.User;public class LoginAction2  {private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String aaa() throws Exception {//public String execute() throws Exception {if ("admin".equals(user.getUserName()) && "admin".equals(user.getPassword())){System.out.println("登录成功");return "success";}else{System.out.println("登录失败");return "error";}}}
注:Struts2中的action类可以不继承任何类,这是与struts1最大的不同,也正是这一点使得struts2晋级为轻量级、低侵入性框架。在struts2中action类的核心函数不再需要大量的参数,这给测试带来了很大的好处。


Login.jsp中的内容

<%@ page language="java" contentType="text/html; charset=GB18030"    pageEncoding="GB18030"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030"><title>Insert title here</title></head><body><form action="user/Login.action" method="post"><input name="user.userName" type="text"><br><input name="user.password" type="text"><br><input type="submit" value="登录"></form></body></html>
注:虽然struts1和struts2的web.xml内容配置不同,但jsp中的action还是木有什么本质的区别的。

执行结果展示




以上就是一个简单的struts2登录实例,关于struts2,还有待于进一步深入研究。



原创粉丝点击