struts2入门例子

 

首先先说下,最近感觉baidu空间老是抽风,用得很不爽!

struts入门案例,我也跟风,写个登录的吧,嘿嘿..

步骤1. 新建一个web project,就取名为struts2吧,

步骤2, 导入struts2需要的包. 导入到WebContent下的web-inf下的lib

步骤3, 在src下面新建一个包,包名为:com.test.action

步骤4,在src下面新建一个File,取名为strut2.xml,这个也就是struts2里面的一个重要配置文件,文件内容如下:

<?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="struts2" extends="struts-default">
   <action name="login" class="com.test.action.LoginAction">
   <result name="input">login.jsp</result>
   <result>/result.jsp</result>
   <result name="failer">/login.jsp</result>
   </action>
   </package>

</struts>

步骤5, 在步骤3建好的包下面新建一个class文件,LoginAction.java

代码如下:

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{      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("hello".equals(this.getUsername().trim())&&"word".equals(this.getPassword().trim()))
        {
            return "success";
        }
        else
        {
            this.addFieldError("username", "username or password error!!!!");
            return "failer";
        }
    }
  
    public void validate() {    //验证
      
        if(null==this.getUsername()|| "".equals(this.getUsername().trim()))
        {
            this.addFieldError("username", "username request");
        }
        if(null==this.getPassword()||"".equals(this.getPassword().trim()))
        {
            this.addFieldError("password", "password request");
        }
    }
  

}

步骤6,Webcontent下的web-inf下面的web.xml进行配置,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">
<filter>
<filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

步骤7,在WebContent下面新建两个jsp文件,分别命名为login.jsp 和 result.jsp

其中login.jsp的代码为:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit name="submit"></s:submit>
</s:form>
</body>
</html>

result.jsp的代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'result.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>
    username:${requestScope.username}<br>
    password:${requestScope.password}
</body>
</html>

完成以上几个步骤,整个项目的目录如下:

struts2入门例子_第1张图片

步骤8,部署发布

步骤9,打开ie,输入: http://localhost:8080/struts2/login.jsp OK,输入hello和 word,然后提交,即可看到效果啦~~~

 

 

 

目前布置成功,但是输入: http://localhost:8080/struts2/login.jsp时显示的是空白页!

如果在配置过程中出现:错误可以大胆的更换一下struts2的jar包,我忘记什么错误了,大概是:missmappingexception

 

你可能感兴趣的:(html,struts,String,encoding,stylesheet,passwords)