傻瓜式struts2入门小例子

最近几天,都没做什么,做个struts2小例子,顺便复习一下,以后再整合SSH

 

环境:MyEclipse6.5、JDK1.6、Tomcat6.0

 

1.新建一个工程,如图

 

傻瓜式struts2入门小例子_第1张图片

 

2.导入struts2所需的包


傻瓜式struts2入门小例子_第2张图片
 

3.新建包和类及jsp文件,结构如下


傻瓜式struts2入门小例子_第3张图片

<?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">

	<!--  定义Struts2的FilterDispathcer的Filter  -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>
	</filter>
	<!--  FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

<?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="mypackage" extends="struts-default">
		<action name="login" class="chenlh.action.LoginAction">
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>

 

package chenlh.action;

import chenlh.bean.User;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String execute() throws Exception {
		if (user.getUsername().equals("chenlh")
				&& user.getUserpwd().equals("chenlh"))
			return SUCCESS;
		else
			return ERROR;
	}
}

 

 

package chenlh.bean;

public class User {

	private String username;
	private String userpwd;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUserpwd() {
		return userpwd;
	}

	public void setUserpwd(String userpwd) {
		this.userpwd = userpwd;
	}
}

 

 

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>登录</title>
	</head>
	<body>
		<s:form action="login">
			<s:textfield name="user.username" label="用户名"></s:textfield>
			<s:password name="user.userpwd" label="密码"></s:password>
			<s:submit value="提交"></s:submit>
		</s:form>
	</body>
</html>

 

 

3.6success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
    <head>
        <title>登录成功</title>
    </head>
    <body>
        登录成功!
    </body>
</html>

 

 

3.7error.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
    <head>
        <title>登录失败</title>
    </head>
    <body>
        用户名密码错误
    </body>
</html>

 

4.好了一些都OK,开始发布
傻瓜式struts2入门小例子_第4张图片

点击提交


5.测试成功

 

6.如果tomcat启动的时候出现这种禁告


 

解决办法:

这是默认语言环境没有配置:有两种方法可以解决

在WEB-INF/struts.properties或者src/struts.properties文件中如下配置:

struts.locale=zh_CN

或者在struts.xml中如下配置;

<constant name="struts.locale" value="zh_CN" /> 


 这是网站找的解决方法, 我试过第一种可以解决,第二种解决不了,还是会出现警告,反正不管了,哈

 

7.有什么问题或疑问及时交流,我也是个初学者,呵呵
 

你可能感兴趣的:(jsp,xml,Web,struts,javaee)