struts2.0学习笔记二(struts2.0第一个程序)

前提配置完后台struts2.0的运行环境

 

1.helloworld.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!--该taglib标签默认包含在了struts2.0的包中了-->
<!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>
    <s:form action="login">
     <s:textfield name="user" label="username"></s:textfield>
     <s:textfield name="pwd" label="password"></s:textfield>
     <s:submit label="submit"></s:submit>
    </s:form>
  </body>
</html>
2.配置struts.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="struts2" extends="struts-default">
        <action name="login" class="com.test.action.LoginAction">
            <result name="success">/result.jsp</result>
            <result name="input">/index.jsp</result>
        </action>        
    </package>
    <!--该LOGIN和你页面提交的login要相对应-->
</struts>
3.编写com.test.action.LoginAction

package com.test.action;

import com.bean.Point;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
 
 String username;
 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;
 }
 
 @Override
 public String execute() throws Exception {
  return "success";
 }
 

}
4.显示的result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  
   
    <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>  
   username:<s:property value="username"/>
   password:<s:property value="password"/>

<!--该username和password和你action中的属性名一定要对应,他相当于是调用了action中相对应属性的get方法-->
  </body>
</html>

你可能感兴趣的:(html,jsp,cache,css,struts)