Struts2入门(一)

来源:互联网 发布:好用的粉底液推荐知乎 编辑:程序博客网 时间:2024/06/14 19:01

1:helloworld.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Hello World</title>
</head>
<body>
<h4>Hello World From Struts2</h4>
   <form action="hello" method="post">
      <label for="name">Please enter your name</label><br/>
      <input type="text" name="name"/>
      <input type="submit" value="你好"/>
   </form>
</body>
</html>


2:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置 Struts2 的 Filter -->
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
</web-app>


3:struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
      <!-- 配置 Struts 可以受理的请求的扩展名 -->
      <constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.action.extension" value="action,do,"></constant>
  <package name="hellodemo" extends="struts-default" namespace="/">
  <action name="hello" class="cn.itcast.action.HelloAction" method="execute">
  <result name="success">/hello.jsp</result>
  </action> 
  </package> 
</struts>


4:HelloAction

package cn.itcast.action;
/*
 * 访问action,每次访问action时候,默认执行名称为execute方法
 */
public class HelloAction {
private String name;
  public String execute() throws Exception {
     return "success";
  }
  public String getName() {
     return name;
  }
  public void setName(String name) {
     this.name = name;
  }


}


5:hello.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello World<s:property value="name"/>
</body>
</html>

原创粉丝点击