HttpServletRequest獲取URL get變量

helloController.javacss

package cn.han.controller;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class helloController {
	@RequestMapping("/hello/1")
	public ModelAndView hello(HttpServletRequest req,HttpServletResponse rep){
		String var1=req.getParameter("var1");
		ModelAndView mV=new ModelAndView();
		mV.addObject("msg","Hello Spring Mvc Annotation Demo");
		mV.addObject("var1", var1);
		mV.setViewName("hello");
		return mV;
	}

}

WEB-INF/jsp/hello.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hello.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>
    This is my JSP page. <br>
    ${msg}<br/>
    ${var1}
  </body>
</html>

運行,輸入

http://han-pc:8080/03Springmvc_Demo/hello/1.html?var1=This GET Parameter
html

輸出java

This is my JSP page.
Hello Spring Mvc Annotation Demo
This GET Parameter 
web

下面講一個常見的部署錯誤解決辦法spring

An internal error occurred during: "Launching XXXX on Tomcat  6.x".
java.lang.NullPointerException
mvc

錯誤報告:
java.lang.NullPointerException
at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(Unknown Source)
at com.genuitec.eclipse.ast.deploy.core.ModuleDeployment.<init>(Unknown Source)
at com.genuitec.eclipse.ast.deploy.core.WebDeployment.<init>(Unknown Source)
at com.genuitec.eclipse.ast.deploy.core.Deployment.create(Unknown Source)
at com.genuitec.eclipse.ast.deploy.core.DeploymentManager.loadFromPreferences(Unknown Source)
at com.genuitec.eclipse.ast.deploy.core.DeploymentManager.init(Unknown Source)
at com.genuitec.eclipse.ast.deploy.core.DeploymentManager.<init>(Unknown Source)
at com.genuitec.eclipse.ast.deploy.core.DeploymentManager.getDefault(Unknown Source)
at com.genuitec.eclipse.ast.deploy.core.ui.ServerTreeViewer$ServerTreeContentProvider.inputChanged(Unknown Source)
at org.eclipse.jface.viewers.ContentViewer.setInput(ContentViewer.java:276)
at org.eclipse.jface.viewers.StructuredViewer.setInput(StructuredViewer.java:1690)
at com.genuitec.eclipse.ast.deploy.core.ui.ServerTreeViewer.<init>(Unknown Source)
at com.genuitec.eclipse.ast.deploy.core.ui.ServerView.createPartControl(Unknown Source)
at org.eclipse.ui.internal.ViewReference.createPartHelper(ViewReference.java:375)
at org.eclipse.ui.internal.ViewReference.createPart(ViewReference.java:229)
at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:595)
at org.eclipse.ui.handlers.ShowViewHandler.openView(ShowViewHandler.java:145)
at org.eclipse.ui.handlers.ShowViewHandler.execute(ShowViewHandler.java:76)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:293)
app

一、首先關閉MyEclipse工做空間。
二、而後刪除工做空間下的
「/.metadata/.plugins/org.eclipse.core.runtime/.settings/com.genuitec.eclipse.ast.deploy.core.prefs」
這個文件。在這個文件中存儲了一些部署項目的信息。刪了這個部署的項目的信息就沒有錯誤了。
三、重啓啓動MyEclipse便可解決了。
eclipse

相關文章
相關標籤/搜索