struts2之HelloWorld

来源:互联网 发布:焦作淘宝实体店地 编辑:程序博客网 时间:2024/06/06 02:48

struts1到struts2的变化,可谓是从内到外,变了个彻底,今天来学习学习struts2.按照估计管理一切起始于helloworld。

使用MyEclipse8.5工具进行自动化开发

IDE环境:jdk1.6+tomcat6.0+struts2.1
1.选择菜单栏的File->NEW->Web Project
2.在弹出的对话框的Project Name中输入hellostruts其他默认就好,点finish(忽略提示信息点yes)
3.在刚建的项目hellostruts上右击,选择MyEclipse->Add Struts Capabilities....
4.在弹出的对话框中的Struts specification右方选择struts2.1,其他使用默认点Finish
添加后的项目中在三个地方会有变化
     ①在src下会多出个struts.xml
     ②在项目下会多出一堆struts的jarbao
     ③在web.xml中会多出一个配置action过滤器的配置信息
<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>*.action</url-pattern>  </filter-mapping>
5.在WebRoot下新建一个index.jsp、welcome.jsp和error.jsp
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>   <form action="login.action" method="post">   用户名:<input type="text" name="username" /><br/>   密码:<input type="password" name="userpass" /><br/>   <input type="submit" value="登录" />   </form>  </body></html>


welcome.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'welcome.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    欢迎您,<%=request.getAttribute("userName") %>  </body></html>
error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'error.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    对不起,在响应过程中发生了错误,错误信息如下:<br />    <%=request.getAttribute("errInfo") %>  </body></html>
6.新建一个包com.touhou.web.struts.actions,在包下新建LoginAction.java
LoginAction.java:
package com.touhou.web.struts.actions;import com.opensymphony.xwork2.ActionSupport;//继承ActionSupport类public class LoginAction extends ActionSupport {//用户名private String userName;//密码private String userPass;//错误提示信息private String errInfo;//设置三个方法用于Action将这两个属性放入request域中public String getUserName(){return userName;}public String getUserPass() {return userPass;}public String getErrInfo() {return errInfo;}//接受户名信息public void setUserName(String userName) {this.userName = userName;}//接受密码public void setUserPass(String userPass) {this.userPass = userPass;}//设置一个方法验证登录public String login() {System.out.println("userName:"+userName);System.out.println("userPass:"+userPass);//当且仅当用户名为admin密码为123时跳往SUCCESS所对应的页面if("admin".equals(userName)&&"123".equals(userPass)) {return SUCCESS;}else{errInfo = "您输入的用户名有误";return ERROR;}}}
7.找到src下的struts.xml,右击选择Open With->Text Editor,拷入下面的代码
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><span style="white-space:pre"></span><!-- 新建一个包,包的命名空间为mystruts,继承自struts-default --><span style="white-space:pre"></span><package name="mystruts" extends="struts-default"><span style="white-space:pre"></span><!-- 添加一个action,用index.action访问即可访问到这个action,这个action会直接跳转到index.jsp --><span style="white-space:pre"></span><action name="index"><span style="white-space:pre"></span><result>/index.jsp</result><span style="white-space:pre"></span></action><span style="white-space:pre"></span><span style="white-space:pre"></span><!-- 添加一个action,用login.action访问,它会直接访问到LoginAction.class这个类 --><span style="white-space:pre"></span><action name="login" class="com.touhou.web.struts.actions.LoginAction" method="login"><span style="white-space:pre"></span><result name="login">/welcome.jsp</result><span style="white-space:pre"></span><result name="error">/error.jsp</result><span style="white-space:pre"></span></action><span style="white-space:pre"></span></package></struts>    
8.将项目发布到Tomcat上,运行http://localhost:8080/hellostruts/index.action访问即可

原理图
第一次请求index.action:

第二次请求login.action:

0 0
原创粉丝点击