eclipse web helloworld

eclipse中创建动态web工程

1. Java Resource中创建包及类HelloWorldAction

eclipse web helloworld_第1张图片

类名HelloWorldAction

/*
 * Created on 2006-3-23
 * TODO
 * author:gf
 */
package com.gc.action;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;


// 实现了controller接口
public class HelloWorldAction implements Controller {
private Logger logger = Logger.getLogger(this.getClass().getName());
private String helloWorld;
private String viewPage;

public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Map model = new HashMap();
model.put("helloWorld", getHelloWorld());
return new ModelAndView(getViewPage(), model);
}


//依赖注入要返回的页面
public void setViewPage(String viewPage) {
this.viewPage = viewPage;
}


//获取要返回的页面
public String getViewPage() {
return viewPage;
}


//依赖注入也显示在页面中的文字
public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}


//获取要显示在页面中的文字
public String getHelloWorld() {
return helloWorld;
}
}


2.dispatcherServlet-servlet.xml





    
   
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">


helloWorldAction





class="org.springframework.web.servlet.view.InternalResourceViewResolver">

org.springframework.web.servlet.view.InternalResourceView







   

   HelloWorld TTTT




   /WEB-INF/jsp/index.jsp






3.web.xml


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">
 
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
/WEB-INF/dispatcherServlet-servlet.xml


1






dispatcherServlet
*.do


   
        index.jsp    
 
   





4.log4j.properties文件放哪里合适?

eclipse web helloworld_第2张图片


log4j.rootLogger=DEBUG,stdout,R


log4j.logger.org=ERROR, A1


log4j.logger.com.gc.action=DEBUG,A2


log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=org.log
log4j.appender.A1.MaxFileSize=500KB
log4j.appender.A1.MaxBackupIndex=50
log4j.appender.A1.Append=true
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ISO8601} - [%p] [%C{1}] - %m%n




log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=gc.log
log4j.appender.A2.MaxFileSize=500KB
log4j.appender.A2.MaxBackupIndex=50
log4j.appender.A2.Append=true
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d{ISO8601} - [%p] [%C{1}] - %m%n


#--------------------stdout--------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout


# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %c - %m%n


#--------------------R--------------------------------
#log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
#this log file will be stored in web server's /bin directory,modify to your path which want to store.
log4j.appender.R.File=gf.log
#log4j.appender.R.datePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.R.datePattern='.'yyyy-MM-dd
log4j.appender.R.append=true
## Keep one backup file
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %c - %m%n
#[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n


5.index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




Test Spring Web



<% String str = (String) request.getAttribute("helloWorld"); %>


<%=str %>







6.第一天运行程序时,将本机中的tomcat关闭。eclipse中访问http://localhost:8080/spring-test/helloWorld.do,程序正确运行。


第二天再运行,发现一直提示错误,我在任务管理器中删除javaw.exe,仍然无法运行。后将WebContent目录下的index.jsp拷贝到WebContent/WEB-INF/jsp/目录下,程序正确运行了。但是,我第一天运行成功的前提是将WebContent/WEB-INF/jsp/目录下的index.jsp拷贝到WebContent目录下。

你可能感兴趣的:(java)