I have made a simple spring mvc application using spring 3.1.my objective isto implement the spring-security feature in my project.security part is working fine but I'm having problem in getting the username entered by user within my spring controller class(Java class).
I know it is very easy with jsp,we used to achieve this through request.getParameter("input component name") but since it is spring I am not getting the valu inside my controller using this very syntax,so,I decided to use @RequestParam.I'm attaching my code herewith.My jsp page is login.jsp as follows
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Login Page</title>
<style>
.errorblock {
color:red;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
</style>
</head>
<body onload='document.f.j_username.focus();'>
<h3>Login with Username and Password (Custom Page)</h3>
<c:if test="${not empty error}">
<div class="errorblock">
Your login attempt was not successful, try again.<br /> Caused by:
${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
</div>
</c:if>
<form name='f' action="<c:url value='j_spring_security_check'/>"
method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='j_username'>
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='j_password'/>
</td>
</tr>
<tr>
<td><input name="submit" type="submit"
value="Submit" />
</td>
<td><input name="reset" type="reset" />
</td>
</tr>
</table>
</form>
</body>
</html>
from the field 'j_username' we want to get the value entered by the user inside our controller.Now I'm attaching my Controller class named ContactController.java as follows
package com.edifixio.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.edifixio.model.Contact;
import com.edifixio.service.InContactService;
@Controller
public class ContactController{
private InContactService inContactService;
public InContactService getInContactService() {
return inContactService;
}
@Autowired
public void setInContactService(InContactService inContactService) {
this.inContactService = inContactService;
}
@RequestMapping(value = "/index")
public String login() {
return "login";
}
@RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
public String loginError() {
return "login";
}
@RequestMapping(value = "/logout")
public String logout() {
return "login";
}
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String listManagers(Map<String, Object> map,@RequestParam String j_username){
System.out.println("User="+j_username);
map.put("contact", new Contact());
map.put("contactList", inContactService.showAllManager());
return "allcontact";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String storeManager(@ModelAttribute("contact") Contact contact,
BindingResult bindingResult) {
inContactService.addContact(contact);
return "redirect:/index";
}
}
Now I'm getting the error with following controller code
public String listManagers(Map<String, Object> map,@RequestParam String j_username){
System.out.println("User="+j_username);
map.put("contact", new Contact());
map.put("contactList", inContactService.showAllManager());
return "allcontact";
}
I am getting the error
HTTP Status 400 -
type Status report
description: The request sent by the client was syntactically incorrect ().
I have tried with the following code to optimize the error::
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String listManagers(Map<String, Object>map,@RequestParam(required=false) String j_username){
System.out.println("User="+j_username);
map.put("contact", new Contact());
map.put("contactList", inContactService.showAllManager());
return "allcontact";
}
using this I was able to bypass the server error 400 but couldn't retrieve the user name within the above mentioned controller
here is my spring-security.xml file
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http auto-config="true">
<intercept-url pattern="/login" access="ROLE_ADMIN" />
<form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="SELECT user_name,user_password,account_status FROM systemuser WHERE user_name=?"
authorities-by-username-query="SELECT user_name,authority FROM systemuser WHERE user_name=?"/>
</authentication-provider>
</authentication-manager>
</beans:beans>
can anyone has any feasible solution to this?????????