第一个Struts2程序,helloworld

第一个struts2程序,整了将近一天,好激动。嘿嘿

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
  <!-- 配置Struts 2框架的核心Filter -->
  <filter>
   <!-- 配置Struts 2框架的核心Filter的名字 -->
   <filter-name>struts</filter-name>
   <!--配置Struts 2框架的核心Filter的实现类  -->
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <!-- 配置Filter拦截的URL -->
  <filter-mapping>
   <filter-name>struts</filter-name>
   <!-- 配置struts2的核心 FilterDispatcher拦截所有用户请求-->
   <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
  <welcome-file>s2impl/login.jsp</welcome-file>
  </welcome-file-list>
 
</web-app>


struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.locale" value="zh_CN"/>
<constant name="struts.il8n.encoding" value="gb2312"/>

<package name="helloworld" namespace="" extends="struts-default">
<action name="helloworldAction" class="cn.javass.hello.Struts2impl.action.HelloWorldAction">
<result name="toWelcome">/s2impl/welcome.jsp</result>
</action>
</package>

</struts>



login.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!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>


<form action="/helloworld/helloworldAction.action" method="post">
<input type="hidden" name="submitFlag" value="login"/>
账号::<input type="text" name="account"><br>
密码::<input type="text" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>


welcome.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!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:property value="account"/>的朋友来访

</body>
</html>


HelloWorldAction.java
package cn.javass.hello.Struts2impl.action;

import com.opensymphony.xwork2.Action;

public class HelloWorldAction implements Action{
private String account;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSubmitFlag() {
return submitFlag;
}
public void setSubmitFlag(String submitFlag) {
this.submitFlag = submitFlag;
}
private String password;
private String submitFlag;
public String execute() throws Exception{
System.out.println("用户输入的参数为===account="+account+",password="+password+",submitFlag="+submitFlag);
return "toWelcome";
}

}


这是项目目录结构


第一个Struts2程序,helloworld

你可能感兴趣的:(java,html,struts)